]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
Implement -Wimplicit-fallthrough.
[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"
cb18fd07 49#include "spellcheck.h"
cb60f38d 50
81a75f0f 51cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 52
8fba1830
BRF
53/* Mode used to build pointers (VOIDmode means ptr_mode). */
54
55machine_mode c_default_pointer_mode = VOIDmode;
56
7f4edbcb 57/* The following symbols are subsumed in the c_global_trees array, and
d125d268 58 listed here individually for documentation purposes.
7f4edbcb
BS
59
60 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
61
62 tree short_integer_type_node;
63 tree long_integer_type_node;
64 tree long_long_integer_type_node;
65
66 tree short_unsigned_type_node;
67 tree long_unsigned_type_node;
68 tree long_long_unsigned_type_node;
69
de7df9eb
JM
70 tree truthvalue_type_node;
71 tree truthvalue_false_node;
72 tree truthvalue_true_node;
7f4edbcb
BS
73
74 tree ptrdiff_type_node;
75
76 tree unsigned_char_type_node;
77 tree signed_char_type_node;
78 tree wchar_type_node;
7f4edbcb 79
b6baa67d
KVH
80 tree char16_type_node;
81 tree char32_type_node;
82
7f4edbcb
BS
83 tree float_type_node;
84 tree double_type_node;
85 tree long_double_type_node;
86
87 tree complex_integer_type_node;
88 tree complex_float_type_node;
89 tree complex_double_type_node;
90 tree complex_long_double_type_node;
91
9a8ce21f
JG
92 tree dfloat32_type_node;
93 tree dfloat64_type_node;
94 tree_dfloat128_type_node;
95
7f4edbcb
BS
96 tree intQI_type_node;
97 tree intHI_type_node;
98 tree intSI_type_node;
99 tree intDI_type_node;
100 tree intTI_type_node;
101
102 tree unsigned_intQI_type_node;
103 tree unsigned_intHI_type_node;
104 tree unsigned_intSI_type_node;
105 tree unsigned_intDI_type_node;
106 tree unsigned_intTI_type_node;
107
108 tree widest_integer_literal_type_node;
109 tree widest_unsigned_literal_type_node;
110
111 Nodes for types `void *' and `const void *'.
112
113 tree ptr_type_node, const_ptr_type_node;
114
115 Nodes for types `char *' and `const char *'.
116
117 tree string_type_node, const_string_type_node;
118
119 Type `char[SOMENUMBER]'.
120 Used when an array of char is needed and the size is irrelevant.
121
122 tree char_array_type_node;
123
7f4edbcb
BS
124 Type `wchar_t[SOMENUMBER]' or something like it.
125 Used when a wide string literal is created.
126
127 tree wchar_array_type_node;
128
b6baa67d
KVH
129 Type `char16_t[SOMENUMBER]' or something like it.
130 Used when a UTF-16 string literal is created.
131
132 tree char16_array_type_node;
133
134 Type `char32_t[SOMENUMBER]' or something like it.
135 Used when a UTF-32 string literal is created.
136
137 tree char32_array_type_node;
138
7f4edbcb
BS
139 Type `int ()' -- used for implicit declaration of functions.
140
141 tree default_function_type;
142
7f4edbcb
BS
143 A VOID_TYPE node, packaged in a TREE_LIST.
144
145 tree void_list_node;
146
684d9f3b 147 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
148 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
149 VAR_DECLS, but C++ does.)
63ad61ed 150
0ba8a114 151 tree function_name_decl_node;
684d9f3b 152 tree pretty_function_name_decl_node;
0ba8a114
NS
153 tree c99_function_name_decl_node;
154
155 Stack of nested function name VAR_DECLs.
35b1a6fa 156
0ba8a114 157 tree saved_function_name_decls;
63ad61ed 158
7f4edbcb
BS
159*/
160
161tree c_global_trees[CTI_MAX];
17211ab5 162\f
4078b403
NB
163/* Switches common to the C front ends. */
164
63973df3
NB
165/* Nonzero means don't output line number information. */
166
167char flag_no_line_commands;
168
169/* Nonzero causes -E output not to be done, but directives such as
170 #define that have side effects are still obeyed. */
171
172char flag_no_output;
173
174/* Nonzero means dump macros in some fashion. */
175
176char flag_dump_macros;
177
178/* Nonzero means pass #include lines through to the output. */
179
180char flag_dump_includes;
181
c0d578e6
GK
182/* Nonzero means process PCH files while preprocessing. */
183
184bool flag_pch_preprocess;
185
17211ab5
GK
186/* The file name to which we should write a precompiled header, or
187 NULL if no header will be written in this compile. */
188
189const char *pch_file;
190
3df89291
NB
191/* Nonzero if an ISO standard was selected. It rejects macros in the
192 user's namespace. */
193int flag_iso;
194
4078b403
NB
195/* C/ObjC language option variables. */
196
197
4078b403
NB
198/* Nonzero means allow type mismatches in conditional expressions;
199 just make their values `void'. */
200
201int flag_cond_mismatch;
202
203/* Nonzero means enable C89 Amendment 1 features. */
204
205int flag_isoc94;
206
48b0b196 207/* Nonzero means use the ISO C99 (or C11) dialect of C. */
4078b403
NB
208
209int flag_isoc99;
210
48b0b196 211/* Nonzero means use the ISO C11 dialect of C. */
2778d766 212
48b0b196 213int flag_isoc11;
2778d766 214
6614fd40 215/* Nonzero means that we have builtin functions, and main is an int. */
4078b403
NB
216
217int flag_hosted = 1;
218
4078b403
NB
219
220/* ObjC language option variables. */
221
222
4078b403
NB
223/* Tells the compiler that this is a special run. Do not perform any
224 compiling, instead we are to test some platform dependent features
225 and output a C header file with appropriate definitions. */
226
227int print_struct_values;
228
fa10beec 229/* Tells the compiler what is the constant string class for ObjC. */
4078b403
NB
230
231const char *constant_string_class_name;
232
4078b403
NB
233
234/* C++ language option variables. */
235
a75f1574
JM
236/* The reference version of the ABI for -Wabi. */
237
238int warn_abi_version = -1;
4078b403 239
4078b403
NB
240/* Nonzero means generate separate instantiation control files and
241 juggle them at link time. */
242
243int flag_use_repository;
244
129211bc 245/* The C++ dialect being used. Default set in c_common_post_options. */
966541e3 246
129211bc 247enum cxx_dialect cxx_dialect = cxx_unset;
966541e3 248
4afe7ad7 249/* Maximum template instantiation depth. This limit exists to limit the
3ff60975 250 time it takes to notice excessively recursive template instantiations.
4078b403 251
3ff60975
JM
252 The default is lower than the 1024 recommended by the C++0x standard
253 because G++ runs out of stack before 1024 with highly recursive template
254 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
255
256int max_tinst_depth = 900;
4078b403 257
f09f1de5
MM
258/* The elements of `ridpointers' are identifier nodes for the reserved
259 type names and storage classes. It is indexed by a RID_... value. */
260tree *ridpointers;
261
c2255bc4 262tree (*make_fname_decl) (location_t, tree, int);
2ce07e2d 263
7d882b83
ILT
264/* Nonzero means don't warn about problems that occur when the code is
265 executed. */
266int c_inhibit_evaluation_warnings;
e78a3b42 267
5386338c
JM
268/* Whether we are building a boolean conversion inside
269 convert_for_assignment, or some other late binary operation. If
270 build_binary_op is called for C (from code shared by C and C++) in
271 this case, then the operands have already been folded and the
272 result will not be folded again, so C_MAYBE_CONST_EXPR should not
273 be generated. */
274bool in_late_binary_op;
275
148e4216
JM
276/* Whether lexing has been completed, so subsequent preprocessor
277 errors should use the compiler's input_location. */
278bool done_lexing = false;
279
ec5c56db 280/* Information about how a function name is generated. */
0ba8a114
NS
281struct fname_var_t
282{
8b60264b
KG
283 tree *const decl; /* pointer to the VAR_DECL. */
284 const unsigned rid; /* RID number for the identifier. */
285 const int pretty; /* How pretty is it? */
0ba8a114
NS
286};
287
ec5c56db 288/* The three ways of getting then name of the current function. */
0ba8a114
NS
289
290const struct fname_var_t fname_vars[] =
291{
ec5c56db 292 /* C99 compliant __func__, must be first. */
0ba8a114 293 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 294 /* GCC __FUNCTION__ compliant. */
0ba8a114 295 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 296 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
297 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
298 {NULL, 0, 0},
299};
300
c98cd5bf
JM
301/* Global visibility options. */
302struct visibility_flags visibility_options;
303
62e4eb35 304static tree check_case_value (location_t, tree);
b155cfd9
MP
305static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
306 bool *);
4724b3de 307
35b1a6fa
AJ
308static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
309static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
310static tree handle_common_attribute (tree *, tree, tree, int, bool *);
311static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52bf96d2
JH
312static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
313static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
e664c61c
KS
314static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
315 int, bool *);
77bc5132
JJ
316static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
317 int, bool *);
ce6923c5
MP
318static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
319 bool *);
5434dc07 320static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 321static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
86631ea3 322static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
185c9e56 323static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
46a4da10 324static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
325static tree handle_always_inline_attribute (tree *, tree, tree, int,
326 bool *);
d752cfdb
JJ
327static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
328static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0691d1d4 329static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
d2af6a68 330static tree handle_error_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 331static tree handle_used_attribute (tree *, tree, tree, int, bool *);
ce91e74c
JH
332static tree handle_externally_visible_attribute (tree *, tree, tree, int,
333 bool *);
7861b648
AK
334static tree handle_no_reorder_attribute (tree *, tree, tree, int,
335 bool *);
35b1a6fa
AJ
336static tree handle_const_attribute (tree *, tree, tree, int, bool *);
337static tree handle_transparent_union_attribute (tree *, tree, tree,
338 int, bool *);
ee45a32d
EB
339static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
340 int, bool *);
35b1a6fa
AJ
341static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
342static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
343static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
344static tree handle_section_attribute (tree *, tree, tree, int, bool *);
345static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
346static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4bb794e2 347static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
ba885ec5
NS
348static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
349static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 350static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
a0203ca7 351static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
35b1a6fa
AJ
352static tree handle_visibility_attribute (tree *, tree, tree, int,
353 bool *);
354static tree handle_tls_model_attribute (tree *, tree, tree, int,
355 bool *);
356static tree handle_no_instrument_function_attribute (tree *, tree,
357 tree, int, bool *);
1225d6b1
ML
358static tree handle_no_profile_instrument_function_attribute (tree *, tree,
359 tree, int, bool *);
35b1a6fa 360static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
6e9a3221 361static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
362static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
363 bool *);
364static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
0a35513e
AH
365static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
366static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
dcd6de6d 367static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
368static tree handle_deprecated_attribute (tree *, tree, tree, int,
369 bool *);
370static tree handle_vector_size_attribute (tree *, tree, tree, int,
371 bool *);
372static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
373static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
374static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
72954a4f
JM
375static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
376 bool *);
3d091dac 377static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5d32c25 378static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
51bc54a6 379static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
8fcbce72
JJ
380static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
381static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
5779e713 382static tree handle_target_attribute (tree *, tree, tree, int, bool *);
3b1661a9 383static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
ab442df7 384static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
0a35513e 385static tree ignore_attribute (tree *, tree, tree, int, bool *);
7458026b 386static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
0b7b376d 387static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
2a99e5e6 388static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
826cacfe 389static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
390static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
391 bool *);
fff77217 392static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
393static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
394 bool *);
976d5a22 395static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
d5e254e1
IE
396static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
397static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
398static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
81fea426 399static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 400
35b1a6fa
AJ
401static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
402static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
403static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
d07605f5 404static int resort_field_decl_cmp (const void *, const void *);
b34c7881 405
eea1139b
ILT
406/* Reserved words. The third field is a mask: keywords are disabled
407 if they match the mask.
408
409 Masks for languages:
410 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
411 C --std=c99: D_CXXONLY | D_OBJC
412 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
413 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
414 C++ --std=c0x: D_CONLY | D_OBJC
415 ObjC++ is like C++ except that D_OBJC is not set
416
417 If -fno-asm is used, D_ASM is added to the mask. If
418 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
419 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
1973201f 420 In C with -Wc++-compat, we warn if D_CXXWARN is set.
eea1139b 421
1973201f
NP
422 Note the complication of the D_CXX_OBJC keywords. These are
423 reserved words such as 'class'. In C++, 'class' is a reserved
424 word. In Objective-C++ it is too. In Objective-C, it is a
425 reserved word too, but only if it follows an '@' sign.
426*/
eea1139b
ILT
427const struct c_common_resword c_common_reswords[] =
428{
d19fa6b5
JM
429 { "_Alignas", RID_ALIGNAS, D_CONLY },
430 { "_Alignof", RID_ALIGNOF, D_CONLY },
267bac10 431 { "_Atomic", RID_ATOMIC, D_CONLY },
eea1139b
ILT
432 { "_Bool", RID_BOOL, D_CONLY },
433 { "_Complex", RID_COMPLEX, 0 },
939b37da
BI
434 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
435 { "_Cilk_sync", RID_CILK_SYNC, 0 },
9a771876 436 { "_Cilk_for", RID_CILK_FOR, 0 },
fa5da7de 437 { "_Imaginary", RID_IMAGINARY, D_CONLY },
c65699ef
JM
438 { "_Float16", RID_FLOAT16, D_CONLY },
439 { "_Float32", RID_FLOAT32, D_CONLY },
440 { "_Float64", RID_FLOAT64, D_CONLY },
441 { "_Float128", RID_FLOAT128, D_CONLY },
442 { "_Float32x", RID_FLOAT32X, D_CONLY },
443 { "_Float64x", RID_FLOAT64X, D_CONLY },
444 { "_Float128x", RID_FLOAT128X, D_CONLY },
eea1139b
ILT
445 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
446 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
447 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
448 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
449 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
450 { "_Sat", RID_SAT, D_CONLY | D_EXT },
32912286 451 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
bbceee64 452 { "_Noreturn", RID_NORETURN, D_CONLY },
433cc7b0 453 { "_Generic", RID_GENERIC, D_CONLY },
582d9b50 454 { "_Thread_local", RID_THREAD, D_CONLY },
eea1139b
ILT
455 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
456 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
457 { "__alignof", RID_ALIGNOF, 0 },
458 { "__alignof__", RID_ALIGNOF, 0 },
459 { "__asm", RID_ASM, 0 },
460 { "__asm__", RID_ASM, 0 },
461 { "__attribute", RID_ATTRIBUTE, 0 },
462 { "__attribute__", RID_ATTRIBUTE, 0 },
38b7bc7f 463 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
4daba884 464 { "__bases", RID_BASES, D_CXXONLY },
74893f25
RH
465 { "__builtin_call_with_static_chain",
466 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
eea1139b 467 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
d4a83c10 468 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
9e1a8dd1 469 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
eea1139b
ILT
470 { "__builtin_offsetof", RID_OFFSETOF, 0 },
471 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
472 { "__builtin_va_arg", RID_VA_ARG, 0 },
473 { "__complex", RID_COMPLEX, 0 },
474 { "__complex__", RID_COMPLEX, 0 },
475 { "__const", RID_CONST, 0 },
476 { "__const__", RID_CONST, 0 },
477 { "__decltype", RID_DECLTYPE, D_CXXONLY },
4daba884 478 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
eea1139b
ILT
479 { "__extension__", RID_EXTENSION, 0 },
480 { "__func__", RID_C99_FUNCTION_NAME, 0 },
481 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
482 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
483 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
484 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
485 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
486 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
487 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
488 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
3c0d13bf
PC
489 { "__imag", RID_IMAGPART, 0 },
490 { "__imag__", RID_IMAGPART, 0 },
491 { "__inline", RID_INLINE, 0 },
492 { "__inline__", RID_INLINE, 0 },
eea1139b
ILT
493 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
494 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
495 { "__is_class", RID_IS_CLASS, D_CXXONLY },
eea1139b
ILT
496 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
497 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
b3908fcc 498 { "__is_final", RID_IS_FINAL, D_CXXONLY },
3c0d13bf 499 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
eea1139b
ILT
500 { "__is_pod", RID_IS_POD, D_CXXONLY },
501 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
971e17ff 502 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c32097d8
JM
503 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
504 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
dd5d5481
JM
505 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
506 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
b752325e 507 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
eea1139b 508 { "__is_union", RID_IS_UNION, D_CXXONLY },
eea1139b
ILT
509 { "__label__", RID_LABEL, 0 },
510 { "__null", RID_NULL, 0 },
511 { "__real", RID_REALPART, 0 },
512 { "__real__", RID_REALPART, 0 },
513 { "__restrict", RID_RESTRICT, 0 },
514 { "__restrict__", RID_RESTRICT, 0 },
515 { "__signed", RID_SIGNED, 0 },
516 { "__signed__", RID_SIGNED, 0 },
517 { "__thread", RID_THREAD, 0 },
0a35513e
AH
518 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
519 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
520 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
eea1139b
ILT
521 { "__typeof", RID_TYPEOF, 0 },
522 { "__typeof__", RID_TYPEOF, 0 },
3c0d13bf 523 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
eea1139b
ILT
524 { "__volatile", RID_VOLATILE, 0 },
525 { "__volatile__", RID_VOLATILE, 0 },
36a85135
JM
526 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
527 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
528 { "asm", RID_ASM, D_ASM },
529 { "auto", RID_AUTO, 0 },
03c3034e 530 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
531 { "break", RID_BREAK, 0 },
532 { "case", RID_CASE, 0 },
2696a995 533 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
eea1139b 534 { "char", RID_CHAR, 0 },
36a85135
JM
535 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
536 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995 537 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
eea1139b 538 { "const", RID_CONST, 0 },
36a85135 539 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
540 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
541 { "continue", RID_CONTINUE, 0 },
36a85135 542 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b 543 { "default", RID_DEFAULT, 0 },
2696a995 544 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
545 { "do", RID_DO, 0 },
546 { "double", RID_DOUBLE, 0 },
547 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
548 { "else", RID_ELSE, 0 },
549 { "enum", RID_ENUM, 0 },
2696a995
KG
550 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
551 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
eea1139b 552 { "extern", RID_EXTERN, 0 },
2696a995 553 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
554 { "float", RID_FLOAT, 0 },
555 { "for", RID_FOR, 0 },
2696a995 556 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
557 { "goto", RID_GOTO, 0 },
558 { "if", RID_IF, 0 },
559 { "inline", RID_INLINE, D_EXT89 },
560 { "int", RID_INT, 0 },
561 { "long", RID_LONG, 0 },
562 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
2696a995
KG
563 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
564 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
36a85135
JM
565 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
566 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
567 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
568 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
569 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
570 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
eea1139b
ILT
571 { "register", RID_REGISTER, 0 },
572 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
573 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
574 { "return", RID_RETURN, 0 },
575 { "short", RID_SHORT, 0 },
576 { "signed", RID_SIGNED, 0 },
577 { "sizeof", RID_SIZEOF, 0 },
578 { "static", RID_STATIC, 0 },
36a85135 579 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
580 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
581 { "struct", RID_STRUCT, 0 },
582 { "switch", RID_SWITCH, 0 },
2696a995
KG
583 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
584 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
36a85135 585 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
586 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
587 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
588 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
eea1139b 589 { "typedef", RID_TYPEDEF, 0 },
2696a995
KG
590 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
591 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
592 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
593 { "union", RID_UNION, 0 },
594 { "unsigned", RID_UNSIGNED, 0 },
2696a995
KG
595 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
596 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
597 { "void", RID_VOID, 0 },
598 { "volatile", RID_VOLATILE, 0 },
599 { "wchar_t", RID_WCHAR, D_CXXONLY },
600 { "while", RID_WHILE, 0 },
971e17ff 601
b8fd7909
JM
602 /* C++ transactional memory. */
603 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
604 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
605 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
606 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
607
971e17ff
AS
608 /* Concepts-related keywords */
609 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
610 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
611
eea1139b
ILT
612 /* These Objective-C keywords are recognized only immediately after
613 an '@'. */
614 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
615 { "defs", RID_AT_DEFS, D_OBJC },
616 { "encode", RID_AT_ENCODE, D_OBJC },
617 { "end", RID_AT_END, D_OBJC },
618 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
619 { "interface", RID_AT_INTERFACE, D_OBJC },
620 { "protocol", RID_AT_PROTOCOL, D_OBJC },
621 { "selector", RID_AT_SELECTOR, D_OBJC },
622 { "finally", RID_AT_FINALLY, D_OBJC },
92902b1b
IS
623 { "optional", RID_AT_OPTIONAL, D_OBJC },
624 { "required", RID_AT_REQUIRED, D_OBJC },
668ea4b1 625 { "property", RID_AT_PROPERTY, D_OBJC },
c37d8c30 626 { "package", RID_AT_PACKAGE, D_OBJC },
da57d1b9
NP
627 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
628 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
eea1139b
ILT
629 /* These are recognized only in protocol-qualifier context
630 (see above) */
631 { "bycopy", RID_BYCOPY, D_OBJC },
632 { "byref", RID_BYREF, D_OBJC },
633 { "in", RID_IN, D_OBJC },
634 { "inout", RID_INOUT, D_OBJC },
635 { "oneway", RID_ONEWAY, D_OBJC },
636 { "out", RID_OUT, D_OBJC },
668ea4b1 637 /* These are recognized inside a property attribute list */
200290f2
NP
638 { "assign", RID_ASSIGN, D_OBJC },
639 { "copy", RID_COPY, D_OBJC },
668ea4b1 640 { "getter", RID_GETTER, D_OBJC },
200290f2
NP
641 { "nonatomic", RID_NONATOMIC, D_OBJC },
642 { "readonly", RID_READONLY, D_OBJC },
643 { "readwrite", RID_READWRITE, D_OBJC },
644 { "retain", RID_RETAIN, D_OBJC },
668ea4b1 645 { "setter", RID_SETTER, D_OBJC },
eea1139b
ILT
646};
647
648const unsigned int num_c_common_reswords =
649 sizeof c_common_reswords / sizeof (struct c_common_resword);
650
d5e254e1
IE
651/* Table of machine-independent attributes common to all C-like languages.
652
653 All attributes referencing arguments should be additionally processed
654 in chkp_copy_function_type_adding_bounds for correct instrumentation
655 by Pointer Bounds Checker.
656 Current list of processed common attributes: nonnull. */
349ae713
NB
657const struct attribute_spec c_common_attribute_table[] =
658{
62d784f7
KT
659 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
660 affects_type_identity } */
349ae713 661 { "packed", 0, 0, false, false, false,
62d784f7 662 handle_packed_attribute , false},
349ae713 663 { "nocommon", 0, 0, true, false, false,
62d784f7 664 handle_nocommon_attribute, false},
349ae713 665 { "common", 0, 0, true, false, false,
62d784f7 666 handle_common_attribute, false },
349ae713
NB
667 /* FIXME: logically, noreturn attributes should be listed as
668 "false, true, true" and apply to function types. But implementing this
669 would require all the places in the compiler that use TREE_THIS_VOLATILE
670 on a decl to identify non-returning functions to be located and fixed
671 to check the function type instead. */
672 { "noreturn", 0, 0, true, false, false,
62d784f7 673 handle_noreturn_attribute, false },
349ae713 674 { "volatile", 0, 0, true, false, false,
62d784f7 675 handle_noreturn_attribute, false },
5434dc07
MD
676 { "stack_protect", 0, 0, true, false, false,
677 handle_stack_protect_attribute, false },
349ae713 678 { "noinline", 0, 0, true, false, false,
62d784f7 679 handle_noinline_attribute, false },
86631ea3 680 { "noclone", 0, 0, true, false, false,
62d784f7 681 handle_noclone_attribute, false },
185c9e56
ML
682 { "no_icf", 0, 0, true, false, false,
683 handle_noicf_attribute, false },
46a4da10 684 { "leaf", 0, 0, true, false, false,
62d784f7 685 handle_leaf_attribute, false },
349ae713 686 { "always_inline", 0, 0, true, false, false,
62d784f7 687 handle_always_inline_attribute, false },
4eb7fd83 688 { "gnu_inline", 0, 0, true, false, false,
62d784f7 689 handle_gnu_inline_attribute, false },
d752cfdb 690 { "artificial", 0, 0, true, false, false,
62d784f7 691 handle_artificial_attribute, false },
0691d1d4 692 { "flatten", 0, 0, true, false, false,
62d784f7 693 handle_flatten_attribute, false },
349ae713 694 { "used", 0, 0, true, false, false,
62d784f7 695 handle_used_attribute, false },
349ae713 696 { "unused", 0, 0, false, false, false,
62d784f7 697 handle_unused_attribute, false },
ce91e74c 698 { "externally_visible", 0, 0, true, false, false,
62d784f7 699 handle_externally_visible_attribute, false },
7861b648
AK
700 { "no_reorder", 0, 0, true, false, false,
701 handle_no_reorder_attribute, false },
349ae713
NB
702 /* The same comments as for noreturn attributes apply to const ones. */
703 { "const", 0, 0, true, false, false,
62d784f7 704 handle_const_attribute, false },
ee45a32d
EB
705 { "scalar_storage_order", 1, 1, false, false, false,
706 handle_scalar_storage_order_attribute, false },
349ae713 707 { "transparent_union", 0, 0, false, false, false,
62d784f7 708 handle_transparent_union_attribute, false },
fc8600f9 709 { "constructor", 0, 1, true, false, false,
62d784f7 710 handle_constructor_attribute, false },
fc8600f9 711 { "destructor", 0, 1, true, false, false,
62d784f7 712 handle_destructor_attribute, false },
349ae713 713 { "mode", 1, 1, false, true, false,
62d784f7 714 handle_mode_attribute, false },
349ae713 715 { "section", 1, 1, true, false, false,
62d784f7 716 handle_section_attribute, false },
349ae713 717 { "aligned", 0, 1, false, false, false,
62d784f7 718 handle_aligned_attribute, false },
349ae713 719 { "weak", 0, 0, true, false, false,
62d784f7 720 handle_weak_attribute, false },
4bb794e2
ST
721 { "noplt", 0, 0, true, false, false,
722 handle_noplt_attribute, false },
ba885ec5 723 { "ifunc", 1, 1, true, false, false,
62d784f7 724 handle_ifunc_attribute, false },
349ae713 725 { "alias", 1, 1, true, false, false,
62d784f7 726 handle_alias_attribute, false },
a0203ca7 727 { "weakref", 0, 1, true, false, false,
62d784f7 728 handle_weakref_attribute, false },
349ae713 729 { "no_instrument_function", 0, 0, true, false, false,
62d784f7
KT
730 handle_no_instrument_function_attribute,
731 false },
1225d6b1
ML
732 { "no_profile_instrument_function", 0, 0, true, false, false,
733 handle_no_profile_instrument_function_attribute,
734 false },
349ae713 735 { "malloc", 0, 0, true, false, false,
62d784f7 736 handle_malloc_attribute, false },
6e9a3221 737 { "returns_twice", 0, 0, true, false, false,
62d784f7 738 handle_returns_twice_attribute, false },
349ae713 739 { "no_stack_limit", 0, 0, true, false, false,
62d784f7 740 handle_no_limit_stack_attribute, false },
349ae713 741 { "pure", 0, 0, true, false, false,
62d784f7 742 handle_pure_attribute, false },
0a35513e
AH
743 { "transaction_callable", 0, 0, false, true, false,
744 handle_tm_attribute, false },
745 { "transaction_unsafe", 0, 0, false, true, false,
b8fd7909 746 handle_tm_attribute, true },
0a35513e 747 { "transaction_safe", 0, 0, false, true, false,
b8fd7909
JM
748 handle_tm_attribute, true },
749 { "transaction_safe_dynamic", 0, 0, true, false, false,
0a35513e
AH
750 handle_tm_attribute, false },
751 { "transaction_may_cancel_outer", 0, 0, false, true, false,
752 handle_tm_attribute, false },
753 /* ??? These two attributes didn't make the transition from the
754 Intel language document to the multi-vendor language document. */
755 { "transaction_pure", 0, 0, false, true, false,
756 handle_tm_attribute, false },
757 { "transaction_wrap", 1, 1, true, false, false,
758 handle_tm_wrap_attribute, false },
dcd6de6d
ZD
759 /* For internal use (marking of builtins) only. The name contains space
760 to prevent its usage in source code. */
761 { "no vops", 0, 0, true, false, false,
62d784f7 762 handle_novops_attribute, false },
9b86d6bb 763 { "deprecated", 0, 1, false, false, false,
62d784f7 764 handle_deprecated_attribute, false },
349ae713 765 { "vector_size", 1, 1, false, true, false,
f3365c12 766 handle_vector_size_attribute, true },
d7afec4b 767 { "visibility", 1, 1, false, false, false,
62d784f7 768 handle_visibility_attribute, false },
dce81a1a 769 { "tls_model", 1, 1, true, false, false,
62d784f7 770 handle_tls_model_attribute, false },
b34c7881 771 { "nonnull", 0, -1, false, true, true,
62d784f7 772 handle_nonnull_attribute, false },
39f2f3c8 773 { "nothrow", 0, 0, true, false, false,
62d784f7
KT
774 handle_nothrow_attribute, false },
775 { "may_alias", 0, 0, false, true, false, NULL, false },
0bfa5f65 776 { "cleanup", 1, 1, true, false, false,
62d784f7 777 handle_cleanup_attribute, false },
72954a4f 778 { "warn_unused_result", 0, 0, false, true, true,
62d784f7 779 handle_warn_unused_result_attribute, false },
254986c7 780 { "sentinel", 0, 1, false, true, true,
62d784f7 781 handle_sentinel_attribute, false },
b5d32c25
KG
782 /* For internal use (marking of builtins) only. The name contains space
783 to prevent its usage in source code. */
784 { "type generic", 0, 0, false, true, true,
62d784f7 785 handle_type_generic_attribute, false },
51bc54a6 786 { "alloc_size", 1, 2, false, true, true,
62d784f7 787 handle_alloc_size_attribute, false },
52bf96d2 788 { "cold", 0, 0, true, false, false,
62d784f7 789 handle_cold_attribute, false },
52bf96d2 790 { "hot", 0, 0, true, false, false,
62d784f7 791 handle_hot_attribute, false },
77bc5132
JJ
792 { "no_address_safety_analysis",
793 0, 0, true, false, false,
794 handle_no_address_safety_analysis_attribute,
795 false },
e664c61c
KS
796 { "no_sanitize_address", 0, 0, true, false, false,
797 handle_no_sanitize_address_attribute,
798 false },
de35aa66
MS
799 { "no_sanitize_thread", 0, 0, true, false, false,
800 handle_no_sanitize_address_attribute,
801 false },
ce6923c5
MP
802 { "no_sanitize_undefined", 0, 0, true, false, false,
803 handle_no_sanitize_undefined_attribute,
804 false },
d2af6a68 805 { "warning", 1, 1, true, false, false,
62d784f7 806 handle_error_attribute, false },
d2af6a68 807 { "error", 1, 1, true, false, false,
62d784f7 808 handle_error_attribute, false },
5779e713 809 { "target", 1, -1, true, false, false,
62d784f7 810 handle_target_attribute, false },
3b1661a9
ES
811 { "target_clones", 1, -1, true, false, false,
812 handle_target_clones_attribute, false },
ab442df7 813 { "optimize", 1, -1, true, false, false,
62d784f7 814 handle_optimize_attribute, false },
0a35513e
AH
815 /* For internal use only. The leading '*' both prevents its usage in
816 source code and signals that it may be overridden by machine tables. */
817 { "*tm regparm", 0, 0, false, true, true,
818 ignore_attribute, false },
7458026b 819 { "no_split_stack", 0, 0, true, false, false,
62d784f7 820 handle_no_split_stack_attribute, false },
0b7b376d
RG
821 /* For internal use (marking of builtins and runtime functions) only.
822 The name contains space to prevent its usage in source code. */
823 { "fn spec", 1, 1, false, true, true,
62d784f7 824 handle_fnspec_attribute, false },
2a99e5e6
LL
825 { "warn_unused", 0, 0, false, false, false,
826 handle_warn_unused_attribute, false },
826cacfe
MG
827 { "returns_nonnull", 0, 0, false, true, true,
828 handle_returns_nonnull_attribute, false },
acf0174b
JJ
829 { "omp declare simd", 0, -1, true, false, false,
830 handle_omp_declare_simd_attribute, false },
41958c28
BI
831 { "cilk simd function", 0, -1, true, false, false,
832 handle_omp_declare_simd_attribute, false },
e7b69085 833 { "simd", 0, 1, true, false, false,
fff77217 834 handle_simd_attribute, false },
acf0174b
JJ
835 { "omp declare target", 0, 0, true, false, false,
836 handle_omp_declare_target_attribute, false },
4a38b02b
IV
837 { "omp declare target link", 0, 0, true, false, false,
838 handle_omp_declare_target_attribute, false },
8fcbce72
JJ
839 { "alloc_align", 1, 1, false, true, true,
840 handle_alloc_align_attribute, false },
841 { "assume_aligned", 1, 2, false, true, true,
842 handle_assume_aligned_attribute, false },
976d5a22
TT
843 { "designated_init", 0, 0, false, true, false,
844 handle_designated_init_attribute, false },
d5e254e1
IE
845 { "bnd_variable_size", 0, 0, true, false, false,
846 handle_bnd_variable_size_attribute, false },
847 { "bnd_legacy", 0, 0, true, false, false,
848 handle_bnd_legacy, false },
849 { "bnd_instrument", 0, 0, true, false, false,
850 handle_bnd_instrument, false },
81fea426
MP
851 { "fallthrough", 0, 0, false, false, false,
852 handle_fallthrough_attribute, false },
62d784f7 853 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
854};
855
856/* Give the specifications for the format attributes, used by C and all
d5e254e1 857 descendants.
349ae713 858
d5e254e1
IE
859 All attributes referencing arguments should be additionally processed
860 in chkp_copy_function_type_adding_bounds for correct instrumentation
861 by Pointer Bounds Checker.
862 Current list of processed format attributes: format, format_arg. */
349ae713
NB
863const struct attribute_spec c_common_format_attribute_table[] =
864{
62d784f7
KT
865 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
866 affects_type_identity } */
349ae713 867 { "format", 3, 3, false, true, true,
62d784f7 868 handle_format_attribute, false },
349ae713 869 { "format_arg", 1, 1, false, true, true,
62d784f7
KT
870 handle_format_arg_attribute, false },
871 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
872};
873
36c5e70a 874/* Return identifier for address space AS. */
3ef0694c 875
36c5e70a
BE
876const char *
877c_addr_space_name (addr_space_t as)
878{
3ef0694c
UW
879 int rid = RID_FIRST_ADDR_SPACE + as;
880 gcc_assert (ridpointers [rid]);
881 return IDENTIFIER_POINTER (ridpointers [rid]);
36c5e70a
BE
882}
883
ec5c56db 884/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
885
886void
35b1a6fa 887start_fname_decls (void)
7da551a2 888{
0ba8a114
NS
889 unsigned ix;
890 tree saved = NULL_TREE;
35b1a6fa 891
0ba8a114
NS
892 for (ix = 0; fname_vars[ix].decl; ix++)
893 {
894 tree decl = *fname_vars[ix].decl;
7da551a2 895
0ba8a114
NS
896 if (decl)
897 {
c62c040f
RG
898 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
899 saved);
0ba8a114
NS
900 *fname_vars[ix].decl = NULL_TREE;
901 }
902 }
903 if (saved || saved_function_name_decls)
904 /* Normally they'll have been NULL, so only push if we've got a
905 stack, or they are non-NULL. */
906 saved_function_name_decls = tree_cons (saved, NULL_TREE,
907 saved_function_name_decls);
908}
909
325c3691
RH
910/* Finish up the current bindings, adding them into the current function's
911 statement tree. This must be done _before_ finish_stmt_tree is called.
912 If there is no current function, we must be at file scope and no statements
913 are involved. Pop the previous bindings. */
0ba8a114
NS
914
915void
35b1a6fa 916finish_fname_decls (void)
0ba8a114
NS
917{
918 unsigned ix;
325c3691 919 tree stmts = NULL_TREE;
0ba8a114
NS
920 tree stack = saved_function_name_decls;
921
922 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
325c3691 923 append_to_statement_list (TREE_VALUE (stack), &stmts);
35b1a6fa 924
325c3691 925 if (stmts)
0ba8a114 926 {
325c3691 927 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
bfaba7a9 928
325c3691
RH
929 if (TREE_CODE (*bodyp) == BIND_EXPR)
930 bodyp = &BIND_EXPR_BODY (*bodyp);
6cce57b0 931
86ad3aa9 932 append_to_statement_list_force (*bodyp, &stmts);
325c3691 933 *bodyp = stmts;
0ba8a114 934 }
35b1a6fa 935
0ba8a114
NS
936 for (ix = 0; fname_vars[ix].decl; ix++)
937 *fname_vars[ix].decl = NULL_TREE;
35b1a6fa 938
0ba8a114 939 if (stack)
7da551a2 940 {
ec5c56db 941 /* We had saved values, restore them. */
0ba8a114
NS
942 tree saved;
943
944 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
945 {
946 tree decl = TREE_PURPOSE (saved);
947 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
35b1a6fa 948
0ba8a114
NS
949 *fname_vars[ix].decl = decl;
950 }
951 stack = TREE_CHAIN (stack);
7da551a2 952 }
0ba8a114
NS
953 saved_function_name_decls = stack;
954}
955
6cce57b0 956/* Return the text name of the current function, suitably prettified
0d0bc036 957 by PRETTY_P. Return string must be freed by caller. */
0ba8a114
NS
958
959const char *
35b1a6fa 960fname_as_string (int pretty_p)
0ba8a114 961{
47ab33b2 962 const char *name = "top level";
0d0bc036 963 char *namep;
46c2514e
TT
964 int vrb = 2, len;
965 cpp_string cstr = { 0, 0 }, strname;
47ab33b2 966
3f75a254 967 if (!pretty_p)
47ab33b2
MA
968 {
969 name = "";
970 vrb = 0;
971 }
972
973 if (current_function_decl)
ae2bcd98 974 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
47ab33b2 975
46c2514e 976 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
0d0bc036 977
46c2514e
TT
978 namep = XNEWVEC (char, len);
979 snprintf (namep, len, "\"%s\"", name);
980 strname.text = (unsigned char *) namep;
981 strname.len = len - 1;
0d0bc036 982
b6baa67d 983 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
46c2514e
TT
984 {
985 XDELETEVEC (namep);
986 return (const char *) cstr.text;
0d0bc036 987 }
0d0bc036
AH
988
989 return namep;
0ba8a114
NS
990}
991
0ba8a114
NS
992/* Return the VAR_DECL for a const char array naming the current
993 function. If the VAR_DECL has not yet been created, create it
994 now. RID indicates how it should be formatted and IDENTIFIER_NODE
995 ID is its name (unfortunately C and C++ hold the RID values of
996 keywords in different places, so we can't derive RID from ID in
3ba09659
AH
997 this language independent code. LOC is the location of the
998 function. */
0ba8a114
NS
999
1000tree
3ba09659 1001fname_decl (location_t loc, unsigned int rid, tree id)
0ba8a114
NS
1002{
1003 unsigned ix;
1004 tree decl = NULL_TREE;
1005
1006 for (ix = 0; fname_vars[ix].decl; ix++)
1007 if (fname_vars[ix].rid == rid)
1008 break;
1009
1010 decl = *fname_vars[ix].decl;
1011 if (!decl)
7da551a2 1012 {
8d3e27d1
DJ
1013 /* If a tree is built here, it would normally have the lineno of
1014 the current statement. Later this tree will be moved to the
1015 beginning of the function and this line number will be wrong.
1016 To avoid this problem set the lineno to 0 here; that prevents
4b7e68e7 1017 it from appearing in the RTL. */
325c3691 1018 tree stmts;
3c20847b 1019 location_t saved_location = input_location;
3c20847b 1020 input_location = UNKNOWN_LOCATION;
35b1a6fa 1021
325c3691 1022 stmts = push_stmt_list ();
c2255bc4 1023 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
325c3691
RH
1024 stmts = pop_stmt_list (stmts);
1025 if (!IS_EMPTY_STMT (stmts))
1026 saved_function_name_decls
1027 = tree_cons (decl, stmts, saved_function_name_decls);
0ba8a114 1028 *fname_vars[ix].decl = decl;
3c20847b 1029 input_location = saved_location;
7da551a2 1030 }
0ba8a114 1031 if (!ix && !current_function_decl)
3ba09659 1032 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
6cce57b0 1033
0ba8a114 1034 return decl;
7da551a2
RS
1035}
1036
b84a3874 1037/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b30f223b
RS
1038
1039tree
35b1a6fa 1040fix_string_type (tree value)
b30f223b 1041{
b84a3874
RH
1042 int length = TREE_STRING_LENGTH (value);
1043 int nchars;
c162c75e
MA
1044 tree e_type, i_type, a_type;
1045
b57062ca 1046 /* Compute the number of elements, for the array type. */
b6baa67d
KVH
1047 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1048 {
1049 nchars = length;
1050 e_type = char_type_node;
1051 }
1052 else if (TREE_TYPE (value) == char16_array_type_node)
1053 {
1054 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1055 e_type = char16_type_node;
1056 }
1057 else if (TREE_TYPE (value) == char32_array_type_node)
1058 {
1059 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1060 e_type = char32_type_node;
1061 }
1062 else
1063 {
1064 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1065 e_type = wchar_type_node;
1066 }
b30f223b 1067
89a42ac8
ZW
1068 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1069 limit in C++98 Annex B is very large (65536) and is not normative,
1070 so we do not diagnose it (warn_overlength_strings is forced off
1071 in c_common_post_options). */
1072 if (warn_overlength_strings)
1073 {
1074 const int nchars_max = flag_isoc99 ? 4095 : 509;
1075 const int relevant_std = flag_isoc99 ? 99 : 90;
1076 if (nchars - 1 > nchars_max)
1077 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1078 separate the %d from the 'C'. 'ISO' should not be
1079 translated, but it may be moved after 'C%d' in languages
1080 where modifiers follow nouns. */
509c9d60 1081 pedwarn (input_location, OPT_Woverlength_strings,
fcf73884 1082 "string length %qd is greater than the length %qd "
89a42ac8
ZW
1083 "ISO C%d compilers are required to support",
1084 nchars - 1, nchars_max, relevant_std);
1085 }
1326a48b 1086
cfb10bd3
GDR
1087 /* Create the array type for the string constant. The ISO C++
1088 standard says that a string literal has type `const char[N]' or
1089 `const wchar_t[N]'. We use the same logic when invoked as a C
1090 front-end with -Wwrite-strings.
1091 ??? We should change the type of an expression depending on the
1092 state of a warning flag. We should just be warning -- see how
1093 this is handled in the C++ front-end for the deprecated implicit
1094 conversion from string literals to `char*' or `wchar_t*'.
c162c75e
MA
1095
1096 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1097 array type being the unqualified version of that type.
1098 Therefore, if we are constructing an array of const char, we must
1099 construct the matching unqualified array type first. The C front
1100 end does not require this, but it does no harm, so we do it
1101 unconditionally. */
c62c040f 1102 i_type = build_index_type (size_int (nchars - 1));
c162c75e 1103 a_type = build_array_type (e_type, i_type);
cfb10bd3 1104 if (c_dialect_cxx() || warn_write_strings)
46df2823 1105 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
d9cf7c82 1106
c162c75e 1107 TREE_TYPE (value) = a_type;
ccd4c832 1108 TREE_CONSTANT (value) = 1;
3521b33c 1109 TREE_READONLY (value) = 1;
b30f223b
RS
1110 TREE_STATIC (value) = 1;
1111 return value;
1112}
88fa5555
DM
1113
1114/* Given a string of type STRING_TYPE, determine what kind of string
1115 token would give an equivalent execution encoding: CPP_STRING,
1116 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
1117 This may not be exactly the string token type that initially created
1118 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
1119 string type at this point.
1120
1121 This effectively reverses part of the logic in lex_string and
1122 fix_string_type. */
1123
1124static enum cpp_ttype
1125get_cpp_ttype_from_string_type (tree string_type)
1126{
1127 gcc_assert (string_type);
e5106e27
DM
1128 if (TREE_CODE (string_type) == POINTER_TYPE)
1129 string_type = TREE_TYPE (string_type);
1130
88fa5555
DM
1131 if (TREE_CODE (string_type) != ARRAY_TYPE)
1132 return CPP_OTHER;
1133
1134 tree element_type = TREE_TYPE (string_type);
1135 if (TREE_CODE (element_type) != INTEGER_TYPE)
1136 return CPP_OTHER;
1137
1138 int bits_per_character = TYPE_PRECISION (element_type);
1139 switch (bits_per_character)
1140 {
1141 case 8:
1142 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
1143 case 16:
1144 return CPP_STRING16;
1145 case 32:
1146 return CPP_STRING32;
1147 }
1148
1149 return CPP_OTHER;
1150}
1151
1152/* The global record of string concatentations, for use in
1153 extracting locations within string literals. */
1154
1155GTY(()) string_concat_db *g_string_concat_db;
1156
e5106e27 1157/* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
88fa5555
DM
1158
1159const char *
e5106e27
DM
1160c_get_substring_location (const substring_loc &substr_loc,
1161 location_t *out_loc)
88fa5555 1162{
e5106e27
DM
1163 enum cpp_ttype tok_type
1164 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
88fa5555
DM
1165 if (tok_type == CPP_OTHER)
1166 return "unrecognized string type";
1167
65e736c0 1168 return get_source_location_for_substring (parse_in, g_string_concat_db,
e5106e27
DM
1169 substr_loc.get_fmt_string_loc (),
1170 tok_type,
1171 substr_loc.get_caret_idx (),
1172 substr_loc.get_start_idx (),
1173 substr_loc.get_end_idx (),
65e736c0 1174 out_loc);
88fa5555
DM
1175}
1176
b30f223b 1177\f
f479b43d
JM
1178/* Fold X for consideration by one of the warning functions when checking
1179 whether an expression has a constant value. */
1180
1181static tree
1182fold_for_warn (tree x)
1183{
1184 if (c_dialect_cxx ())
1185 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1186 else
1187 /* The C front-end has already folded X appropriately. */
1188 return x;
1189}
1190
d74154d5
RS
1191/* Print a warning if a constant expression had overflow in folding.
1192 Invoke this function on every expression that the language
1193 requires to be a constant expression.
1194 Note the ANSI C standard says it is erroneous for a
1195 constant expression to overflow. */
96571883
BK
1196
1197void
35b1a6fa 1198constant_expression_warning (tree value)
393eda6a 1199{
b8698a0f 1200 if (warn_overflow && pedantic
393eda6a
MLI
1201 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1202 || TREE_CODE (value) == FIXED_CST
1203 || TREE_CODE (value) == VECTOR_CST
1204 || TREE_CODE (value) == COMPLEX_CST)
1205 && TREE_OVERFLOW (value))
509c9d60 1206 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
393eda6a
MLI
1207}
1208
1209/* The same as above but print an unconditional error. */
1210void
1211constant_expression_error (tree value)
96571883 1212{
c05f751c 1213 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
ab22c1fa 1214 || TREE_CODE (value) == FIXED_CST
69ef87e2 1215 || TREE_CODE (value) == VECTOR_CST
c05f751c 1216 || TREE_CODE (value) == COMPLEX_CST)
393eda6a
MLI
1217 && TREE_OVERFLOW (value))
1218 error ("overflow in constant expression");
d74154d5
RS
1219}
1220
59c0753d
MLI
1221/* Print a warning if an expression had overflow in folding and its
1222 operands hadn't.
1223
d74154d5
RS
1224 Invoke this function on every expression that
1225 (1) appears in the source code, and
59c0753d 1226 (2) is a constant expression that overflowed, and
d74154d5 1227 (3) is not already checked by convert_and_check;
59c0753d
MLI
1228 however, do not invoke this function on operands of explicit casts
1229 or when the expression is the result of an operator and any operand
1230 already overflowed. */
d74154d5
RS
1231
1232void
c2255bc4 1233overflow_warning (location_t loc, tree value)
d74154d5 1234{
7d882b83
ILT
1235 if (c_inhibit_evaluation_warnings != 0)
1236 return;
59c0753d
MLI
1237
1238 switch (TREE_CODE (value))
69ef87e2 1239 {
59c0753d 1240 case INTEGER_CST:
c2255bc4 1241 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
59c0753d 1242 break;
b8698a0f 1243
59c0753d 1244 case REAL_CST:
c2255bc4
AH
1245 warning_at (loc, OPT_Woverflow,
1246 "floating point overflow in expression");
59c0753d 1247 break;
b8698a0f 1248
ab22c1fa 1249 case FIXED_CST:
c2255bc4 1250 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
ab22c1fa
CF
1251 break;
1252
59c0753d 1253 case VECTOR_CST:
c2255bc4 1254 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
59c0753d 1255 break;
b8698a0f 1256
59c0753d
MLI
1257 case COMPLEX_CST:
1258 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
c2255bc4
AH
1259 warning_at (loc, OPT_Woverflow,
1260 "complex integer overflow in expression");
59c0753d 1261 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
c2255bc4
AH
1262 warning_at (loc, OPT_Woverflow,
1263 "complex floating point overflow in expression");
59c0753d
MLI
1264 break;
1265
1266 default:
1267 break;
69ef87e2 1268 }
d74154d5
RS
1269}
1270
ca409efd
MLI
1271/* Warn about uses of logical || / && operator in a context where it
1272 is likely that the bitwise equivalent was intended by the
1273 programmer. We have seen an expression in which CODE is a binary
a243fb4a
MLI
1274 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1275 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
63a08740 1276void
a243fb4a 1277warn_logical_operator (location_t location, enum tree_code code, tree type,
b8698a0f 1278 enum tree_code code_left, tree op_left,
ca409efd 1279 enum tree_code ARG_UNUSED (code_right), tree op_right)
63a08740 1280{
a243fb4a
MLI
1281 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1282 int in0_p, in1_p, in_p;
1283 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1284 bool strict_overflow_p = false;
1285
ca409efd
MLI
1286 if (code != TRUTH_ANDIF_EXPR
1287 && code != TRUTH_AND_EXPR
1288 && code != TRUTH_ORIF_EXPR
1289 && code != TRUTH_OR_EXPR)
1290 return;
1291
b8787813
MP
1292 /* We don't want to warn if either operand comes from a macro
1293 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1294 see PR61534. */
1295 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1296 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1297 return;
1298
ca409efd
MLI
1299 /* Warn if &&/|| are being used in a context where it is
1300 likely that the bitwise equivalent was intended by the
1301 programmer. That is, an expression such as op && MASK
1302 where op should not be any boolean expression, nor a
1303 constant, and mask seems to be a non-boolean integer constant. */
f479b43d
JM
1304 if (TREE_CODE (op_right) == CONST_DECL)
1305 /* An enumerator counts as a constant. */
1306 op_right = DECL_INITIAL (op_right);
ca409efd
MLI
1307 if (!truth_value_p (code_left)
1308 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1309 && !CONSTANT_CLASS_P (op_left)
1310 && !TREE_NO_WARNING (op_left)
1311 && TREE_CODE (op_right) == INTEGER_CST
1312 && !integer_zerop (op_right)
1313 && !integer_onep (op_right))
63a08740 1314 {
a243fb4a 1315 if (or_op)
ca409efd
MLI
1316 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1317 " applied to non-boolean constant");
1318 else
1319 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1320 " applied to non-boolean constant");
1321 TREE_NO_WARNING (op_left) = true;
a243fb4a
MLI
1322 return;
1323 }
1324
1325 /* We do not warn for constants because they are typical of macro
1326 expansions that test for features. */
f479b43d
JM
1327 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1328 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
a243fb4a
MLI
1329 return;
1330
1331 /* This warning only makes sense with logical operands. */
1332 if (!(truth_value_p (TREE_CODE (op_left))
1333 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1334 || !(truth_value_p (TREE_CODE (op_right))
1335 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1336 return;
1337
3c9aabbd
MG
1338 /* The range computations only work with scalars. */
1339 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1340 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1341 return;
a243fb4a 1342
f2c4a785
MLI
1343 /* We first test whether either side separately is trivially true
1344 (with OR) or trivially false (with AND). If so, do not warn.
1345 This is a common idiom for testing ranges of data types in
1346 portable code. */
1347 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1348 if (!lhs)
1349 return;
1350 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
a243fb4a
MLI
1351 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1352
f2c4a785
MLI
1353 /* If this is an OR operation, invert both sides; now, the result
1354 should be always false to get a warning. */
1355 if (or_op)
1356 in0_p = !in0_p;
1357
1358 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1e537948 1359 if (tem && integer_zerop (tem))
f2c4a785
MLI
1360 return;
1361
1362 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1363 if (!rhs)
1364 return;
1365 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
a243fb4a 1366 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
b8698a0f 1367
f2c4a785
MLI
1368 /* If this is an OR operation, invert both sides; now, the result
1369 should be always false to get a warning. */
a243fb4a 1370 if (or_op)
f2c4a785 1371 in1_p = !in1_p;
b8698a0f 1372
f2c4a785 1373 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1e537948 1374 if (tem && integer_zerop (tem))
f2c4a785
MLI
1375 return;
1376
1377 /* If both expressions have the same operand, if we can merge the
8c2b7f79 1378 ranges, ... */
f2c4a785 1379 if (operand_equal_p (lhs, rhs, 0)
a243fb4a 1380 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
8c2b7f79 1381 in1_p, low1, high1))
a243fb4a 1382 {
8c2b7f79
MP
1383 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1384 /* ... and if the range test is always false, then warn. */
1385 if (tem && integer_zerop (tem))
1386 {
1387 if (or_op)
1388 warning_at (location, OPT_Wlogical_op,
1389 "logical %<or%> of collectively exhaustive tests is "
1390 "always true");
1391 else
1392 warning_at (location, OPT_Wlogical_op,
1393 "logical %<and%> of mutually exclusive tests is "
1394 "always false");
1395 }
1396 /* Or warn if the operands have exactly the same range, e.g.
1397 A > 0 && A > 0. */
a68ae2e1
MP
1398 else if (tree_int_cst_equal (low0, low1)
1399 && tree_int_cst_equal (high0, high1))
8c2b7f79
MP
1400 {
1401 if (or_op)
1402 warning_at (location, OPT_Wlogical_op,
1403 "logical %<or%> of equal expressions");
1404 else
1405 warning_at (location, OPT_Wlogical_op,
1406 "logical %<and%> of equal expressions");
1407 }
63a08740
DM
1408 }
1409}
1410
05b28fd6
MP
1411/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1412 with constant indices. */
1413
1414static tree
1415find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1416{
1417 tree expr = *expr_p;
1418
1419 if ((TREE_CODE (expr) == ARRAY_REF
1420 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1421 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1422 {
1423 *(bool *) data = true;
1424 *walk_subtrees = 0;
1425 }
1426
1427 return NULL_TREE;
1428}
1429
1430/* Warn if a self-comparison always evaluates to true or false. LOC
1431 is the location of the comparison with code CODE, LHS and RHS are
1432 operands of the comparison. */
1433
1434void
1435warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1436{
1437 if (TREE_CODE_CLASS (code) != tcc_comparison)
1438 return;
1439
f2afe6dd
MP
1440 /* Don't warn for various macro expansions. */
1441 if (from_macro_expansion_at (loc)
1442 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1443 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1444 return;
1445
05b28fd6
MP
1446 /* We do not warn for constants because they are typical of macro
1447 expansions that test for features, sizeof, and similar. */
f479b43d
JM
1448 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1449 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
05b28fd6
MP
1450 return;
1451
1452 /* Don't warn for e.g.
1453 HOST_WIDE_INT n;
1454 ...
1455 if (n == (long) n) ...
1456 */
1457 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1458 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1459 return;
1460
173864e8
MP
1461 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1462 It could be a NaN, and NaN never compares equal to anything, even
1463 itself. */
1464 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1465 return;
1466
05b28fd6
MP
1467 if (operand_equal_p (lhs, rhs, 0))
1468 {
1469 /* Don't warn about array references with constant indices;
1470 these are likely to come from a macro. */
1471 bool found = false;
1472 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1473 &found);
1474 if (found)
1475 return;
1476 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1477 || code == GE_EXPR || code == UNLE_EXPR
1478 || code == UNGE_EXPR || code == UNEQ_EXPR);
1479 if (always_true)
1480 warning_at (loc, OPT_Wtautological_compare,
1481 "self-comparison always evaluates to true");
1482 else
1483 warning_at (loc, OPT_Wtautological_compare,
1484 "self-comparison always evaluates to false");
1485 }
1486}
1487
25ff5dd3
MP
1488/* Return true iff T is a boolean promoted to int. */
1489
1490static bool
1491bool_promoted_to_int_p (tree t)
1492{
1493 return (CONVERT_EXPR_P (t)
1494 && TREE_TYPE (t) == integer_type_node
1495 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
1496}
1497
1498/* Return true iff EXPR only contains boolean operands, or comparisons. */
1499
1500static bool
1501expr_has_boolean_operands_p (tree expr)
1502{
1503 STRIP_NOPS (expr);
1504
1505 if (CONVERT_EXPR_P (expr))
1506 return bool_promoted_to_int_p (expr);
1507 else if (UNARY_CLASS_P (expr))
1508 return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0));
1509 else if (BINARY_CLASS_P (expr))
1510 return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0))
1511 && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1)));
1512 else if (COMPARISON_CLASS_P (expr))
1513 return true;
1514 else
1515 return false;
1516}
1517
742938c9
MP
1518/* Warn about logical not used on the left hand side operand of a comparison.
1519 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
3d06b6f2
MP
1520 Do not warn if RHS is of a boolean type, a logical operator, or
1521 a comparison. */
742938c9
MP
1522
1523void
1524warn_logical_not_parentheses (location_t location, enum tree_code code,
ebef225f 1525 tree lhs, tree rhs)
742938c9 1526{
59ea0364
MP
1527 if (TREE_CODE_CLASS (code) != tcc_comparison
1528 || TREE_TYPE (rhs) == NULL_TREE
3d06b6f2
MP
1529 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE
1530 || truth_value_p (TREE_CODE (rhs)))
742938c9
MP
1531 return;
1532
25ff5dd3
MP
1533 /* Don't warn for expression like !x == ~(bool1 | bool2). */
1534 if (expr_has_boolean_operands_p (rhs))
1535 return;
1536
7ccb1a11
JJ
1537 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1538 !(x == 0) or !(y != 0). */
1539 if ((code == EQ_EXPR || code == NE_EXPR)
1540 && integer_zerop (rhs))
1541 return;
1542
ebef225f
MP
1543 if (warning_at (location, OPT_Wlogical_not_parentheses,
1544 "logical not is only applied to the left hand side of "
1545 "comparison")
1546 && EXPR_HAS_LOCATION (lhs))
1547 {
1548 location_t lhs_loc = EXPR_LOCATION (lhs);
1549 rich_location richloc (line_table, lhs_loc);
254830ba
DM
1550 richloc.add_fixit_insert_before (lhs_loc, "(");
1551 richloc.add_fixit_insert_after (lhs_loc, ")");
ebef225f
MP
1552 inform_at_rich_loc (&richloc, "add parentheses around left hand side "
1553 "expression to silence this warning");
1554 }
742938c9 1555}
63a08740 1556
fd4116f4
MLI
1557/* Warn if EXP contains any computations whose results are not used.
1558 Return true if a warning is printed; false otherwise. LOCUS is the
1559 (potential) location of the expression. */
1560
1561bool
1562warn_if_unused_value (const_tree exp, location_t locus)
1563{
1564 restart:
1565 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1566 return false;
1567
1568 /* Don't warn about void constructs. This includes casting to void,
1569 void function calls, and statement expressions with a final cast
1570 to void. */
1571 if (VOID_TYPE_P (TREE_TYPE (exp)))
1572 return false;
1573
1574 if (EXPR_HAS_LOCATION (exp))
1575 locus = EXPR_LOCATION (exp);
1576
1577 switch (TREE_CODE (exp))
1578 {
1579 case PREINCREMENT_EXPR:
1580 case POSTINCREMENT_EXPR:
1581 case PREDECREMENT_EXPR:
1582 case POSTDECREMENT_EXPR:
1583 case MODIFY_EXPR:
1584 case INIT_EXPR:
1585 case TARGET_EXPR:
1586 case CALL_EXPR:
1587 case TRY_CATCH_EXPR:
1588 case WITH_CLEANUP_EXPR:
1589 case EXIT_EXPR:
1590 case VA_ARG_EXPR:
1591 return false;
1592
1593 case BIND_EXPR:
1594 /* For a binding, warn if no side effect within it. */
1595 exp = BIND_EXPR_BODY (exp);
1596 goto restart;
1597
1598 case SAVE_EXPR:
1599 case NON_LVALUE_EXPR:
007a787d 1600 case NOP_EXPR:
fd4116f4
MLI
1601 exp = TREE_OPERAND (exp, 0);
1602 goto restart;
1603
1604 case TRUTH_ORIF_EXPR:
1605 case TRUTH_ANDIF_EXPR:
1606 /* In && or ||, warn if 2nd operand has no side effect. */
1607 exp = TREE_OPERAND (exp, 1);
1608 goto restart;
1609
1610 case COMPOUND_EXPR:
1611 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1612 return true;
1613 /* Let people do `(foo (), 0)' without a warning. */
1614 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1615 return false;
1616 exp = TREE_OPERAND (exp, 1);
1617 goto restart;
1618
1619 case COND_EXPR:
1620 /* If this is an expression with side effects, don't warn; this
1621 case commonly appears in macro expansions. */
1622 if (TREE_SIDE_EFFECTS (exp))
1623 return false;
1624 goto warn;
1625
1626 case INDIRECT_REF:
1627 /* Don't warn about automatic dereferencing of references, since
1628 the user cannot control it. */
1629 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1630 {
1631 exp = TREE_OPERAND (exp, 0);
1632 goto restart;
1633 }
1634 /* Fall through. */
1635
1636 default:
1637 /* Referencing a volatile value is a side effect, so don't warn. */
1638 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1639 && TREE_THIS_VOLATILE (exp))
1640 return false;
1641
1642 /* If this is an expression which has no operands, there is no value
1643 to be unused. There are no such language-independent codes,
1644 but front ends may define such. */
1645 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1646 return false;
1647
1648 warn:
1649 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1650 }
1651}
1652
1653
de9c56a4
RG
1654/* Print a warning about casts that might indicate violation
1655 of strict aliasing rules if -Wstrict-aliasing is used and
3f0a2a47
DM
1656 strict aliasing mode is in effect. OTYPE is the original
1657 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
de9c56a4 1658
79bedddc 1659bool
3f0a2a47 1660strict_aliasing_warning (tree otype, tree type, tree expr)
de9c56a4 1661{
255d3827
RG
1662 /* Strip pointer conversion chains and get to the correct original type. */
1663 STRIP_NOPS (expr);
1664 otype = TREE_TYPE (expr);
1665
ac7ee6ad
RG
1666 if (!(flag_strict_aliasing
1667 && POINTER_TYPE_P (type)
1668 && POINTER_TYPE_P (otype)
1669 && !VOID_TYPE_P (TREE_TYPE (type)))
1670 /* If the type we are casting to is a ref-all pointer
1671 dereferencing it is always valid. */
1672 || TYPE_REF_CAN_ALIAS_ALL (type))
79bedddc
SR
1673 return false;
1674
1675 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
de9c56a4 1676 && (DECL_P (TREE_OPERAND (expr, 0))
79bedddc 1677 || handled_component_p (TREE_OPERAND (expr, 0))))
de9c56a4
RG
1678 {
1679 /* Casting the address of an object to non void pointer. Warn
1680 if the cast breaks type based aliasing. */
79bedddc
SR
1681 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1682 {
1683 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1684 "might break strict-aliasing rules");
1685 return true;
1686 }
de9c56a4
RG
1687 else
1688 {
b8698a0f 1689 /* warn_strict_aliasing >= 3. This includes the default (3).
79bedddc 1690 Only warn if the cast is dereferenced immediately. */
4862826d 1691 alias_set_type set1 =
79bedddc 1692 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
4862826d 1693 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
de9c56a4 1694
4653cae5 1695 if (set1 != set2 && set2 != 0
c06d25bb
RB
1696 && (set1 == 0
1697 || (!alias_set_subset_of (set2, set1)
1698 && !alias_sets_conflict_p (set1, set2))))
79bedddc
SR
1699 {
1700 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1701 "pointer will break strict-aliasing rules");
1702 return true;
1703 }
1704 else if (warn_strict_aliasing == 2
836f7794 1705 && !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1706 {
1707 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1708 "pointer might break strict-aliasing rules");
1709 return true;
1710 }
de9c56a4
RG
1711 }
1712 }
79bedddc
SR
1713 else
1714 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1715 {
1716 /* At this level, warn for any conversions, even if an address is
1717 not taken in the same statement. This will likely produce many
1718 false positives, but could be useful to pinpoint problems that
1719 are not revealed at higher levels. */
4862826d
ILT
1720 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1721 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1722 if (!COMPLETE_TYPE_P (type)
836f7794 1723 || !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1724 {
1725 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1726 "pointer might break strict-aliasing rules");
1727 return true;
1728 }
1729 }
1730
1731 return false;
de9c56a4
RG
1732}
1733
1a4049e7
JJ
1734/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1735 sizeof as last operand of certain builtins. */
1736
1737void
3a785c97 1738sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
9771b263 1739 vec<tree, va_gc> *params, tree *sizeof_arg,
1a4049e7
JJ
1740 bool (*comp_types) (tree, tree))
1741{
1742 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
3a785c97
JJ
1743 bool strop = false, cmp = false;
1744 unsigned int idx = ~0;
1745 location_t loc;
1a4049e7
JJ
1746
1747 if (TREE_CODE (callee) != FUNCTION_DECL
1748 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
9771b263 1749 || vec_safe_length (params) <= 1)
1a4049e7
JJ
1750 return;
1751
1a4049e7
JJ
1752 switch (DECL_FUNCTION_CODE (callee))
1753 {
1754 case BUILT_IN_STRNCMP:
1755 case BUILT_IN_STRNCASECMP:
3a785c97
JJ
1756 cmp = true;
1757 /* FALLTHRU */
1a4049e7 1758 case BUILT_IN_STRNCPY:
3a785c97 1759 case BUILT_IN_STRNCPY_CHK:
1a4049e7 1760 case BUILT_IN_STRNCAT:
3a785c97
JJ
1761 case BUILT_IN_STRNCAT_CHK:
1762 case BUILT_IN_STPNCPY:
1763 case BUILT_IN_STPNCPY_CHK:
1a4049e7
JJ
1764 strop = true;
1765 /* FALLTHRU */
1766 case BUILT_IN_MEMCPY:
3a785c97 1767 case BUILT_IN_MEMCPY_CHK:
1a4049e7 1768 case BUILT_IN_MEMMOVE:
3a785c97 1769 case BUILT_IN_MEMMOVE_CHK:
9771b263 1770 if (params->length () < 3)
3a785c97 1771 return;
9771b263
DN
1772 src = (*params)[1];
1773 dest = (*params)[0];
3a785c97
JJ
1774 idx = 2;
1775 break;
1776 case BUILT_IN_BCOPY:
9771b263 1777 if (params->length () < 3)
3a785c97 1778 return;
9771b263
DN
1779 src = (*params)[0];
1780 dest = (*params)[1];
3a785c97
JJ
1781 idx = 2;
1782 break;
1a4049e7 1783 case BUILT_IN_MEMCMP:
3a785c97 1784 case BUILT_IN_BCMP:
9771b263 1785 if (params->length () < 3)
1a4049e7 1786 return;
9771b263
DN
1787 src = (*params)[1];
1788 dest = (*params)[0];
3a785c97
JJ
1789 idx = 2;
1790 cmp = true;
1a4049e7
JJ
1791 break;
1792 case BUILT_IN_MEMSET:
3a785c97 1793 case BUILT_IN_MEMSET_CHK:
9771b263 1794 if (params->length () < 3)
1a4049e7 1795 return;
9771b263 1796 dest = (*params)[0];
3a785c97
JJ
1797 idx = 2;
1798 break;
1799 case BUILT_IN_BZERO:
9771b263 1800 dest = (*params)[0];
3a785c97 1801 idx = 1;
1a4049e7
JJ
1802 break;
1803 case BUILT_IN_STRNDUP:
9771b263 1804 src = (*params)[0];
1a4049e7 1805 strop = true;
3a785c97
JJ
1806 idx = 1;
1807 break;
1808 case BUILT_IN_MEMCHR:
9771b263 1809 if (params->length () < 3)
3a785c97 1810 return;
9771b263 1811 src = (*params)[0];
3a785c97
JJ
1812 idx = 2;
1813 break;
1814 case BUILT_IN_SNPRINTF:
1815 case BUILT_IN_SNPRINTF_CHK:
1816 case BUILT_IN_VSNPRINTF:
1817 case BUILT_IN_VSNPRINTF_CHK:
9771b263 1818 dest = (*params)[0];
3a785c97
JJ
1819 idx = 1;
1820 strop = true;
1a4049e7
JJ
1821 break;
1822 default:
1823 break;
1824 }
1825
3a785c97
JJ
1826 if (idx >= 3)
1827 return;
1828
1829 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1830 return;
1831
1832 type = TYPE_P (sizeof_arg[idx])
1833 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1834 if (!POINTER_TYPE_P (type))
1835 return;
1836
1a4049e7
JJ
1837 if (dest
1838 && (tem = tree_strip_nop_conversions (dest))
1839 && POINTER_TYPE_P (TREE_TYPE (tem))
1840 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1841 return;
1842
1843 if (src
1844 && (tem = tree_strip_nop_conversions (src))
1845 && POINTER_TYPE_P (TREE_TYPE (tem))
1846 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1847 return;
1848
3a785c97
JJ
1849 loc = sizeof_arg_loc[idx];
1850
1851 if (dest && !cmp)
1a4049e7 1852 {
3a785c97
JJ
1853 if (!TYPE_P (sizeof_arg[idx])
1854 && operand_equal_p (dest, sizeof_arg[idx], 0)
1a4049e7
JJ
1855 && comp_types (TREE_TYPE (dest), type))
1856 {
3a785c97 1857 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
1858 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1859 "argument to %<sizeof%> in %qD call is the same "
1860 "expression as the destination; did you mean to "
1861 "remove the addressof?", callee);
1862 else if ((TYPE_PRECISION (TREE_TYPE (type))
1863 == TYPE_PRECISION (char_type_node))
1864 || strop)
1865 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1866 "argument to %<sizeof%> in %qD call is the same "
1867 "expression as the destination; did you mean to "
1868 "provide an explicit length?", callee);
1869 else
1870 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1871 "argument to %<sizeof%> in %qD call is the same "
1872 "expression as the destination; did you mean to "
1873 "dereference it?", callee);
1874 return;
1875 }
1876
1877 if (POINTER_TYPE_P (TREE_TYPE (dest))
1878 && !strop
1879 && comp_types (TREE_TYPE (dest), type)
1880 && !VOID_TYPE_P (TREE_TYPE (type)))
1881 {
1882 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1883 "argument to %<sizeof%> in %qD call is the same "
1884 "pointer type %qT as the destination; expected %qT "
1885 "or an explicit length", callee, TREE_TYPE (dest),
1886 TREE_TYPE (TREE_TYPE (dest)));
1887 return;
1888 }
1889 }
1890
3a785c97 1891 if (src && !cmp)
1a4049e7 1892 {
3a785c97
JJ
1893 if (!TYPE_P (sizeof_arg[idx])
1894 && operand_equal_p (src, sizeof_arg[idx], 0)
1a4049e7
JJ
1895 && comp_types (TREE_TYPE (src), type))
1896 {
3a785c97 1897 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
1898 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1899 "argument to %<sizeof%> in %qD call is the same "
1900 "expression as the source; did you mean to "
1901 "remove the addressof?", callee);
1902 else if ((TYPE_PRECISION (TREE_TYPE (type))
1903 == TYPE_PRECISION (char_type_node))
1904 || strop)
1905 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1906 "argument to %<sizeof%> in %qD call is the same "
1907 "expression as the source; did you mean to "
1908 "provide an explicit length?", callee);
1909 else
1910 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1911 "argument to %<sizeof%> in %qD call is the same "
1912 "expression as the source; did you mean to "
1913 "dereference it?", callee);
1914 return;
1915 }
1916
1917 if (POINTER_TYPE_P (TREE_TYPE (src))
1918 && !strop
1919 && comp_types (TREE_TYPE (src), type)
1920 && !VOID_TYPE_P (TREE_TYPE (type)))
1921 {
1922 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1923 "argument to %<sizeof%> in %qD call is the same "
1924 "pointer type %qT as the source; expected %qT "
1925 "or an explicit length", callee, TREE_TYPE (src),
1926 TREE_TYPE (TREE_TYPE (src)));
1927 return;
1928 }
1929 }
3a785c97
JJ
1930
1931 if (dest)
1932 {
1933 if (!TYPE_P (sizeof_arg[idx])
1934 && operand_equal_p (dest, sizeof_arg[idx], 0)
1935 && comp_types (TREE_TYPE (dest), type))
1936 {
1937 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1938 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1939 "argument to %<sizeof%> in %qD call is the same "
1940 "expression as the first source; did you mean to "
1941 "remove the addressof?", callee);
1942 else if ((TYPE_PRECISION (TREE_TYPE (type))
1943 == TYPE_PRECISION (char_type_node))
1944 || strop)
1945 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1946 "argument to %<sizeof%> in %qD call is the same "
1947 "expression as the first source; did you mean to "
1948 "provide an explicit length?", callee);
1949 else
1950 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1951 "argument to %<sizeof%> in %qD call is the same "
1952 "expression as the first source; did you mean to "
1953 "dereference it?", callee);
1954 return;
1955 }
1956
1957 if (POINTER_TYPE_P (TREE_TYPE (dest))
1958 && !strop
1959 && comp_types (TREE_TYPE (dest), type)
1960 && !VOID_TYPE_P (TREE_TYPE (type)))
1961 {
1962 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1963 "argument to %<sizeof%> in %qD call is the same "
1964 "pointer type %qT as the first source; expected %qT "
1965 "or an explicit length", callee, TREE_TYPE (dest),
1966 TREE_TYPE (TREE_TYPE (dest)));
1967 return;
1968 }
1969 }
1970
1971 if (src)
1972 {
1973 if (!TYPE_P (sizeof_arg[idx])
1974 && operand_equal_p (src, sizeof_arg[idx], 0)
1975 && comp_types (TREE_TYPE (src), type))
1976 {
1977 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1978 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1979 "argument to %<sizeof%> in %qD call is the same "
1980 "expression as the second source; did you mean to "
1981 "remove the addressof?", callee);
1982 else if ((TYPE_PRECISION (TREE_TYPE (type))
1983 == TYPE_PRECISION (char_type_node))
1984 || strop)
1985 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1986 "argument to %<sizeof%> in %qD call is the same "
1987 "expression as the second source; did you mean to "
1988 "provide an explicit length?", callee);
1989 else
1990 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1991 "argument to %<sizeof%> in %qD call is the same "
1992 "expression as the second source; did you mean to "
1993 "dereference it?", callee);
1994 return;
1995 }
1996
1997 if (POINTER_TYPE_P (TREE_TYPE (src))
1998 && !strop
1999 && comp_types (TREE_TYPE (src), type)
2000 && !VOID_TYPE_P (TREE_TYPE (type)))
2001 {
2002 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2003 "argument to %<sizeof%> in %qD call is the same "
2004 "pointer type %qT as the second source; expected %qT "
2005 "or an explicit length", callee, TREE_TYPE (src),
2006 TREE_TYPE (TREE_TYPE (src)));
2007 return;
2008 }
2009 }
2010
1a4049e7
JJ
2011}
2012
a1e45ff0
DM
2013/* Warn for unlikely, improbable, or stupid DECL declarations
2014 of `main'. */
2015
2016void
2017check_main_parameter_types (tree decl)
2018{
e19a18d4
NF
2019 function_args_iterator iter;
2020 tree type;
a1e45ff0
DM
2021 int argct = 0;
2022
e19a18d4
NF
2023 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2024 {
2025 /* XXX void_type_node belies the abstraction. */
2026 if (type == void_type_node || type == error_mark_node )
2027 break;
2028
f827930a
MP
2029 tree t = type;
2030 if (TYPE_ATOMIC (t))
2031 pedwarn (input_location, OPT_Wmain,
2032 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2033 type, decl);
2034 while (POINTER_TYPE_P (t))
2035 {
2036 t = TREE_TYPE (t);
2037 if (TYPE_ATOMIC (t))
2038 pedwarn (input_location, OPT_Wmain,
2039 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2040 type, decl);
2041 }
2042
e19a18d4
NF
2043 ++argct;
2044 switch (argct)
2045 {
2046 case 1:
2047 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2048 pedwarn (input_location, OPT_Wmain,
2049 "first argument of %q+D should be %<int%>", decl);
2050 break;
2051
2052 case 2:
2053 if (TREE_CODE (type) != POINTER_TYPE
2054 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2055 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2056 != char_type_node))
2057 pedwarn (input_location, OPT_Wmain,
2058 "second argument of %q+D should be %<char **%>", decl);
2059 break;
2060
2061 case 3:
2062 if (TREE_CODE (type) != POINTER_TYPE
2063 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2064 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2065 != char_type_node))
2066 pedwarn (input_location, OPT_Wmain,
2067 "third argument of %q+D should probably be "
2068 "%<char **%>", decl);
2069 break;
2070 }
2071 }
a1e45ff0
DM
2072
2073 /* It is intentional that this message does not mention the third
2074 argument because it's only mentioned in an appendix of the
2075 standard. */
2076 if (argct > 0 && (argct < 2 || argct > 3))
e19a18d4
NF
2077 pedwarn (input_location, OPT_Wmain,
2078 "%q+D takes only zero or two arguments", decl);
38e514c0
MP
2079
2080 if (stdarg_p (TREE_TYPE (decl)))
2081 pedwarn (input_location, OPT_Wmain,
2082 "%q+D declared as variadic function", decl);
a1e45ff0
DM
2083}
2084
0af94e6f
JR
2085/* vector_targets_convertible_p is used for vector pointer types. The
2086 callers perform various checks that the qualifiers are satisfactory,
2087 while OTOH vector_targets_convertible_p ignores the number of elements
2088 in the vectors. That's fine with vector pointers as we can consider,
2089 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2090 and that does not require and conversion of the pointer values.
2091 In contrast, vector_types_convertible_p and
2092 vector_types_compatible_elements_p are used for vector value types. */
f83c7f63
DJ
2093/* True if pointers to distinct types T1 and T2 can be converted to
2094 each other without an explicit cast. Only returns true for opaque
2095 vector types. */
2096bool
2097vector_targets_convertible_p (const_tree t1, const_tree t2)
2098{
31521951 2099 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
b6fc2cdb 2100 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
f83c7f63
DJ
2101 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2102 return true;
2103
2104 return false;
2105}
2106
0af94e6f
JR
2107/* vector_types_convertible_p is used for vector value types.
2108 It could in principle call vector_targets_convertible_p as a subroutine,
2109 but then the check for vector type would be duplicated with its callers,
2110 and also the purpose of vector_targets_convertible_p would become
2111 muddled.
2112 Where vector_types_convertible_p returns true, a conversion might still be
2113 needed to make the types match.
2114 In contrast, vector_targets_convertible_p is used for vector pointer
2115 values, and vector_types_compatible_elements_p is used specifically
2116 in the context for binary operators, as a check if use is possible without
2117 conversion. */
00c8e9f6
MS
2118/* True if vector types T1 and T2 can be converted to each other
2119 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2120 can only be converted with -flax-vector-conversions yet that is not
2121 in effect, emit a note telling the user about that option if such
2122 a note has not previously been emitted. */
2123bool
58f9752a 2124vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
cc27e657 2125{
00c8e9f6 2126 static bool emitted_lax_note = false;
14e765da
JM
2127 bool convertible_lax;
2128
b6fc2cdb 2129 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
14e765da
JM
2130 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2131 return true;
2132
2133 convertible_lax =
2134 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2135 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
a5e0cd1d 2136 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
14e765da
JM
2137 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2138 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
00c8e9f6
MS
2139
2140 if (!convertible_lax || flag_lax_vector_conversions)
2141 return convertible_lax;
2142
2143 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
cf7bc668 2144 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
00c8e9f6
MS
2145 return true;
2146
2147 if (emit_lax_note && !emitted_lax_note)
2148 {
2149 emitted_lax_note = true;
1f5b3869 2150 inform (input_location, "use -flax-vector-conversions to permit "
00c8e9f6
MS
2151 "conversions between vectors with differing "
2152 "element types or numbers of subparts");
2153 }
2154
2155 return false;
cc27e657
PB
2156}
2157
9e1a8dd1
RR
2158/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2159 and have vector types, V0 has the same type as V1, and the number of
2160 elements of V0, V1, MASK is the same.
2161
2162 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2163 called with two arguments. In this case implementation passes the
2164 first argument twice in order to share the same tree code. This fact
2165 could enable the mask-values being twice the vector length. This is
2166 an implementation accident and this semantics is not guaranteed to
2167 the user. */
2168tree
bedc293e
MG
2169c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2170 bool complain)
9e1a8dd1
RR
2171{
2172 tree ret;
2173 bool wrap = true;
2174 bool maybe_const = false;
2175 bool two_arguments = false;
2176
2177 if (v1 == NULL_TREE)
2178 {
2179 two_arguments = true;
2180 v1 = v0;
2181 }
2182
2183 if (v0 == error_mark_node || v1 == error_mark_node
2184 || mask == error_mark_node)
2185 return error_mark_node;
2186
31521951 2187 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
9e1a8dd1 2188 {
bedc293e
MG
2189 if (complain)
2190 error_at (loc, "__builtin_shuffle last argument must "
2191 "be an integer vector");
9e1a8dd1
RR
2192 return error_mark_node;
2193 }
2194
31521951
MP
2195 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2196 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
9e1a8dd1 2197 {
bedc293e
MG
2198 if (complain)
2199 error_at (loc, "__builtin_shuffle arguments must be vectors");
9e1a8dd1
RR
2200 return error_mark_node;
2201 }
2202
2203 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2204 {
bedc293e
MG
2205 if (complain)
2206 error_at (loc, "__builtin_shuffle argument vectors must be of "
2207 "the same type");
9e1a8dd1
RR
2208 return error_mark_node;
2209 }
2210
2211 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2212 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2213 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2214 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2215 {
bedc293e
MG
2216 if (complain)
2217 error_at (loc, "__builtin_shuffle number of elements of the "
2218 "argument vector(s) and the mask vector should "
2219 "be the same");
9e1a8dd1
RR
2220 return error_mark_node;
2221 }
2222
2223 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2224 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2225 {
bedc293e
MG
2226 if (complain)
2227 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2228 "must have the same size as inner type of the mask");
9e1a8dd1
RR
2229 return error_mark_node;
2230 }
2231
2232 if (!c_dialect_cxx ())
2233 {
2234 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2235 v0 = c_fully_fold (v0, false, &maybe_const);
2236 wrap &= maybe_const;
2237
2238 if (two_arguments)
2239 v1 = v0 = save_expr (v0);
2240 else
2241 {
2242 v1 = c_fully_fold (v1, false, &maybe_const);
2243 wrap &= maybe_const;
2244 }
2245
2246 mask = c_fully_fold (mask, false, &maybe_const);
2247 wrap &= maybe_const;
2248 }
bedc293e
MG
2249 else if (two_arguments)
2250 v1 = v0 = save_expr (v0);
9e1a8dd1
RR
2251
2252 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2253
2254 if (!c_dialect_cxx () && !wrap)
2255 ret = c_wrap_maybe_const (ret, true);
2256
2257 return ret;
2258}
2259
828fb3ba
JM
2260/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2261 to integral type. */
2262
2263static tree
2264c_common_get_narrower (tree op, int *unsignedp_ptr)
2265{
2266 op = get_narrower (op, unsignedp_ptr);
2267
2268 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2269 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2270 {
2271 /* C++0x scoped enumerations don't implicitly convert to integral
2272 type; if we stripped an explicit conversion to a larger type we
2273 need to replace it so common_type will still work. */
21fa2faf
RG
2274 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2275 TYPE_UNSIGNED (TREE_TYPE (op)));
828fb3ba
JM
2276 op = fold_convert (type, op);
2277 }
2278 return op;
2279}
2280
6715192c
MLI
2281/* This is a helper function of build_binary_op.
2282
2283 For certain operations if both args were extended from the same
2284 smaller type, do the arithmetic in that type and then extend.
2285
2286 BITWISE indicates a bitwise operation.
2287 For them, this optimization is safe only if
2288 both args are zero-extended or both are sign-extended.
2289 Otherwise, we might change the result.
2290 Eg, (short)-1 | (unsigned short)-1 is (int)-1
b8698a0f 2291 but calculated in (unsigned short) it would be (unsigned short)-1.
6715192c 2292*/
828fb3ba
JM
2293tree
2294shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
6715192c
MLI
2295{
2296 int unsigned0, unsigned1;
2297 tree arg0, arg1;
2298 int uns;
2299 tree type;
2300
2301 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2302 excessive narrowing when we call get_narrower below. For
2303 example, suppose that OP0 is of unsigned int extended
2304 from signed char and that RESULT_TYPE is long long int.
2305 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2306 like
b8698a0f 2307
6715192c
MLI
2308 (long long int) (unsigned int) signed_char
2309
2310 which get_narrower would narrow down to
b8698a0f 2311
6715192c 2312 (unsigned int) signed char
b8698a0f 2313
6715192c
MLI
2314 If we do not cast OP0 first, get_narrower would return
2315 signed_char, which is inconsistent with the case of the
2316 explicit cast. */
2317 op0 = convert (result_type, op0);
2318 op1 = convert (result_type, op1);
2319
828fb3ba
JM
2320 arg0 = c_common_get_narrower (op0, &unsigned0);
2321 arg1 = c_common_get_narrower (op1, &unsigned1);
6715192c
MLI
2322
2323 /* UNS is 1 if the operation to be done is an unsigned one. */
2324 uns = TYPE_UNSIGNED (result_type);
2325
2326 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2327 but it *requires* conversion to FINAL_TYPE. */
b8698a0f 2328
6715192c
MLI
2329 if ((TYPE_PRECISION (TREE_TYPE (op0))
2330 == TYPE_PRECISION (TREE_TYPE (arg0)))
2331 && TREE_TYPE (op0) != result_type)
2332 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2333 if ((TYPE_PRECISION (TREE_TYPE (op1))
2334 == TYPE_PRECISION (TREE_TYPE (arg1)))
2335 && TREE_TYPE (op1) != result_type)
2336 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b8698a0f 2337
6715192c 2338 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
b8698a0f 2339
6715192c
MLI
2340 /* For bitwise operations, signedness of nominal type
2341 does not matter. Consider only how operands were extended. */
2342 if (bitwise)
2343 uns = unsigned0;
b8698a0f 2344
6715192c
MLI
2345 /* Note that in all three cases below we refrain from optimizing
2346 an unsigned operation on sign-extended args.
2347 That would not be valid. */
b8698a0f 2348
6715192c
MLI
2349 /* Both args variable: if both extended in same way
2350 from same width, do it in that width.
2351 Do it unsigned if args were zero-extended. */
2352 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2353 < TYPE_PRECISION (result_type))
2354 && (TYPE_PRECISION (TREE_TYPE (arg1))
2355 == TYPE_PRECISION (TREE_TYPE (arg0)))
2356 && unsigned0 == unsigned1
2357 && (unsigned0 || !uns))
2358 return c_common_signed_or_unsigned_type
2359 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2360
2361 else if (TREE_CODE (arg0) == INTEGER_CST
2362 && (unsigned1 || !uns)
2363 && (TYPE_PRECISION (TREE_TYPE (arg1))
2364 < TYPE_PRECISION (result_type))
2365 && (type
2366 = c_common_signed_or_unsigned_type (unsigned1,
2367 TREE_TYPE (arg1)))
2368 && !POINTER_TYPE_P (type)
2369 && int_fits_type_p (arg0, type))
2370 return type;
2371
2372 else if (TREE_CODE (arg1) == INTEGER_CST
2373 && (unsigned0 || !uns)
2374 && (TYPE_PRECISION (TREE_TYPE (arg0))
2375 < TYPE_PRECISION (result_type))
2376 && (type
2377 = c_common_signed_or_unsigned_type (unsigned0,
2378 TREE_TYPE (arg0)))
2379 && !POINTER_TYPE_P (type)
2380 && int_fits_type_p (arg1, type))
2381 return type;
2382
2383 return result_type;
2384}
2385
7a37fa90
MM
2386/* Returns true iff any integer value of type FROM_TYPE can be represented as
2387 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2388
2389static bool
2390int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2391{
2392 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2393 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2394 REAL_VALUE_TYPE real_low_bound =
2395 real_value_from_int_cst (0, type_low_bound);
2396 REAL_VALUE_TYPE real_high_bound =
2397 real_value_from_int_cst (0, type_high_bound);
2398
2399 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2400 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2401}
2402
2403/* Checks if expression EXPR of complex/real/integer type cannot be converted
2404 to the complex/real/integer type TYPE. Function returns non-zero when:
68fca595
MP
2405 * EXPR is a constant which cannot be exactly converted to TYPE.
2406 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
7a37fa90
MM
2407 for EXPR type and TYPE being both integers or both real, or both
2408 complex.
2409 * EXPR is not a constant of complex type and TYPE is a real or
2410 an integer.
68fca595
MP
2411 * EXPR is not a constant of real type and TYPE is an integer.
2412 * EXPR is not a constant of integer type which cannot be
2413 exactly converted to real type.
7a37fa90 2414
0e3a99ae 2415 Function allows conversions between types of different signedness and
49b0aa18 2416 can return SAFE_CONVERSION (zero) in that case. Function can produce
7a37fa90
MM
2417 signedness warnings if PRODUCE_WARNS is true.
2418
2419 Function allows conversions from complex constants to non-complex types,
2420 provided that imaginary part is zero and real part can be safely converted
2421 to TYPE. */
68fca595 2422
49b0aa18 2423enum conversion_safety
68fca595 2424unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
422c3a54 2425{
49b0aa18 2426 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
374035cb 2427 tree expr_type = TREE_TYPE (expr);
68fca595 2428 loc = expansion_point_location_if_in_system_header (loc);
422c3a54 2429
0e3a99ae 2430 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
0011dedb 2431 {
7a37fa90
MM
2432 /* If type is complex, we are interested in compatibility with
2433 underlying type. */
2434 if (TREE_CODE (type) == COMPLEX_TYPE)
2435 type = TREE_TYPE (type);
2436
422c3a54 2437 /* Warn for real constant that is not an exact integer converted
0e3a99ae 2438 to integer type. */
374035cb 2439 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2440 && TREE_CODE (type) == INTEGER_TYPE)
2441 {
2442 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
49b0aa18 2443 give_warning = UNSAFE_REAL;
0e3a99ae 2444 }
91c41804 2445 /* Warn for an integer constant that does not fit into integer type. */
374035cb 2446 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2447 && TREE_CODE (type) == INTEGER_TYPE
2448 && !int_fits_type_p (expr, type))
2449 {
2450 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
374035cb 2451 && tree_int_cst_sgn (expr) < 0)
0e3a99ae
AS
2452 {
2453 if (produce_warns)
2454 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2455 " implicitly converted to unsigned type");
2456 }
2457 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2458 {
2459 if (produce_warns)
2460 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2461 " constant value to negative integer");
2462 }
7060db96 2463 else
49b0aa18 2464 give_warning = UNSAFE_OTHER;
0e3a99ae 2465 }
422c3a54 2466 else if (TREE_CODE (type) == REAL_TYPE)
0e3a99ae
AS
2467 {
2468 /* Warn for an integer constant that does not fit into real type. */
2469 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2470 {
2471 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2472 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2473 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2474 }
2475 /* Warn for a real constant that does not fit into a smaller
2476 real type. */
2477 else if (TREE_CODE (expr_type) == REAL_TYPE
2478 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2479 {
2480 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2481 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2482 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2483 }
2484 }
2485 }
7a37fa90
MM
2486
2487 else if (TREE_CODE (expr) == COMPLEX_CST)
2488 {
2489 tree imag_part = TREE_IMAGPART (expr);
2490 /* Conversion from complex constant with zero imaginary part,
2491 perform check for conversion of real part. */
2492 if ((TREE_CODE (imag_part) == REAL_CST
2493 && real_zerop (imag_part))
2494 || (TREE_CODE (imag_part) == INTEGER_CST
2495 && integer_zerop (imag_part)))
2496 /* Note: in this branch we use recursive call to unsafe_conversion_p
2497 with different type of EXPR, but it is still safe, because when EXPR
2498 is a constant, it's type is not used in text of generated warnings
2499 (otherwise they could sound misleading). */
2500 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2501 produce_warns);
2502 /* Conversion from complex constant with non-zero imaginary part. */
2503 else
2504 {
2505 /* Conversion to complex type.
2506 Perform checks for both real and imaginary parts. */
2507 if (TREE_CODE (type) == COMPLEX_TYPE)
2508 {
2509 /* Unfortunately, produce_warns must be false in two subsequent
2510 calls of unsafe_conversion_p, because otherwise we could
2511 produce strange "double" warnings, if both real and imaginary
2512 parts have conversion problems related to signedness.
2513
2514 For example:
2515 int32_t _Complex a = 0x80000000 + 0x80000000i;
2516
2517 Possible solution: add a separate function for checking
2518 constants and combine result of two calls appropriately. */
2519 enum conversion_safety re_safety =
2520 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2521 enum conversion_safety im_safety =
2522 unsafe_conversion_p (loc, type, imag_part, false);
2523
2524 /* Merge the results into appropriate single warning. */
2525
2526 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2527 if (re_safety == im_safety)
2528 give_warning = re_safety;
2529 else if (!re_safety && im_safety)
2530 give_warning = im_safety;
2531 else if (re_safety && !im_safety)
2532 give_warning = re_safety;
2533 else
2534 give_warning = UNSAFE_OTHER;
2535 }
2536 /* Warn about conversion from complex to real or integer type. */
2537 else
2538 give_warning = UNSAFE_IMAGINARY;
2539 }
2540 }
2541
2542 /* Checks for remaining case: EXPR is not constant. */
0e3a99ae
AS
2543 else
2544 {
422c3a54 2545 /* Warn for real types converted to integer types. */
6715192c 2546 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae 2547 && TREE_CODE (type) == INTEGER_TYPE)
49b0aa18 2548 give_warning = UNSAFE_REAL;
422c3a54 2549
6715192c 2550 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2551 && TREE_CODE (type) == INTEGER_TYPE)
2552 {
cfdaefec 2553 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
c00e8b06 2554 expr = get_unwidened (expr, 0);
6715192c 2555 expr_type = TREE_TYPE (expr);
cfdaefec 2556
6715192c 2557 /* Don't warn for short y; short x = ((int)y & 0xff); */
b8698a0f 2558 if (TREE_CODE (expr) == BIT_AND_EXPR
0e3a99ae 2559 || TREE_CODE (expr) == BIT_IOR_EXPR
6715192c
MLI
2560 || TREE_CODE (expr) == BIT_XOR_EXPR)
2561 {
374035cb
MLI
2562 /* If both args were extended from a shortest type,
2563 use that type if that is safe. */
b8698a0f
L
2564 expr_type = shorten_binary_op (expr_type,
2565 TREE_OPERAND (expr, 0),
2566 TREE_OPERAND (expr, 1),
6715192c
MLI
2567 /* bitwise */1);
2568
6715192c
MLI
2569 if (TREE_CODE (expr) == BIT_AND_EXPR)
2570 {
2571 tree op0 = TREE_OPERAND (expr, 0);
2572 tree op1 = TREE_OPERAND (expr, 1);
9c591bd0
MLI
2573 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2574 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2575
2576 /* If one of the operands is a non-negative constant
2577 that fits in the target type, then the type of the
2578 other operand does not matter. */
6715192c
MLI
2579 if ((TREE_CODE (op0) == INTEGER_CST
2580 && int_fits_type_p (op0, c_common_signed_type (type))
2581 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2582 || (TREE_CODE (op1) == INTEGER_CST
374035cb 2583 && int_fits_type_p (op1, c_common_signed_type (type))
b8698a0f 2584 && int_fits_type_p (op1,
374035cb 2585 c_common_unsigned_type (type))))
49b0aa18 2586 return SAFE_CONVERSION;
9c591bd0
MLI
2587 /* If constant is unsigned and fits in the target
2588 type, then the result will also fit. */
2589 else if ((TREE_CODE (op0) == INTEGER_CST
b8698a0f 2590 && unsigned0
9c591bd0
MLI
2591 && int_fits_type_p (op0, type))
2592 || (TREE_CODE (op1) == INTEGER_CST
2593 && unsigned1
2594 && int_fits_type_p (op1, type)))
49b0aa18 2595 return SAFE_CONVERSION;
6715192c
MLI
2596 }
2597 }
0e3a99ae 2598 /* Warn for integer types converted to smaller integer types. */
b8698a0f 2599 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2600 give_warning = UNSAFE_OTHER;
7060db96
MLI
2601
2602 /* When they are the same width but different signedness,
2603 then the value may change. */
0e3a99ae 2604 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
6715192c 2605 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7060db96
MLI
2606 /* Even when converted to a bigger type, if the type is
2607 unsigned but expr is signed, then negative values
2608 will be changed. */
0e3a99ae
AS
2609 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2610 && produce_warns)
6312e84d
MLI
2611 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2612 "may change the sign of the result",
2613 type, expr_type);
0e3a99ae 2614 }
422c3a54
MLI
2615
2616 /* Warn for integer types converted to real types if and only if
0e3a99ae
AS
2617 all the range of values of the integer type cannot be
2618 represented by the real type. */
6715192c 2619 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2620 && TREE_CODE (type) == REAL_TYPE)
2621 {
58076e21
MLI
2622 /* Don't warn about char y = 0xff; float x = (int) y; */
2623 expr = get_unwidened (expr, 0);
2624 expr_type = TREE_TYPE (expr);
2625
7a37fa90 2626 if (!int_safely_convertible_to_real_p (expr_type, type))
49b0aa18 2627 give_warning = UNSAFE_OTHER;
0e3a99ae 2628 }
422c3a54
MLI
2629
2630 /* Warn for real types converted to smaller real types. */
6715192c 2631 else if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2632 && TREE_CODE (type) == REAL_TYPE
2633 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2634 give_warning = UNSAFE_REAL;
7a37fa90
MM
2635
2636 /* Check conversion between two complex types. */
2637 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2638 && TREE_CODE (type) == COMPLEX_TYPE)
2639 {
2640 /* Extract underlying types (i.e., type of real and imaginary
2641 parts) of expr_type and type. */
2642 tree from_type = TREE_TYPE (expr_type);
2643 tree to_type = TREE_TYPE (type);
2644
2645 /* Warn for real types converted to integer types. */
2646 if (TREE_CODE (from_type) == REAL_TYPE
2647 && TREE_CODE (to_type) == INTEGER_TYPE)
2648 give_warning = UNSAFE_REAL;
2649
2650 /* Warn for real types converted to smaller real types. */
2651 else if (TREE_CODE (from_type) == REAL_TYPE
2652 && TREE_CODE (to_type) == REAL_TYPE
2653 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2654 give_warning = UNSAFE_REAL;
2655
2656 /* Check conversion for complex integer types. Here implementation
2657 is simpler than for real-domain integers because it does not
2658 involve sophisticated cases, such as bitmasks, casts, etc. */
2659 else if (TREE_CODE (from_type) == INTEGER_TYPE
2660 && TREE_CODE (to_type) == INTEGER_TYPE)
2661 {
2662 /* Warn for integer types converted to smaller integer types. */
2663 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2664 give_warning = UNSAFE_OTHER;
2665
2666 /* Check for different signedness, see case for real-domain
2667 integers (above) for a more detailed comment. */
2668 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2669 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2670 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2671 && produce_warns)
2672 warning_at (loc, OPT_Wsign_conversion,
2673 "conversion to %qT from %qT "
2674 "may change the sign of the result",
2675 type, expr_type);
2676 }
2677 else if (TREE_CODE (from_type) == INTEGER_TYPE
2678 && TREE_CODE (to_type) == REAL_TYPE
2679 && !int_safely_convertible_to_real_p (from_type, to_type))
2680 give_warning = UNSAFE_OTHER;
2681 }
2682
2683 /* Warn for complex types converted to real or integer types. */
2684 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2685 && TREE_CODE (type) != COMPLEX_TYPE)
2686 give_warning = UNSAFE_IMAGINARY;
0e3a99ae
AS
2687 }
2688
2689 return give_warning;
2690}
2691
2692/* Warns if the conversion of EXPR to TYPE may alter a value.
2693 This is a helper function for warnings_for_convert_and_check. */
2694
2695static void
68fca595 2696conversion_warning (location_t loc, tree type, tree expr)
0e3a99ae 2697{
0e3a99ae 2698 tree expr_type = TREE_TYPE (expr);
49b0aa18 2699 enum conversion_safety conversion_kind;
422c3a54 2700
49b0aa18 2701 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
0e3a99ae 2702 return;
422c3a54 2703
66f20604
MP
2704 /* This may happen, because for LHS op= RHS we preevaluate
2705 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2706 means we could no longer see the code of the EXPR. */
2707 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2708 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2709 if (TREE_CODE (expr) == SAVE_EXPR)
2710 expr = TREE_OPERAND (expr, 0);
2711
0e3a99ae
AS
2712 switch (TREE_CODE (expr))
2713 {
2714 case EQ_EXPR:
2715 case NE_EXPR:
2716 case LE_EXPR:
2717 case GE_EXPR:
2718 case LT_EXPR:
2719 case GT_EXPR:
2720 case TRUTH_ANDIF_EXPR:
2721 case TRUTH_ORIF_EXPR:
2722 case TRUTH_AND_EXPR:
2723 case TRUTH_OR_EXPR:
2724 case TRUTH_XOR_EXPR:
2725 case TRUTH_NOT_EXPR:
2726 /* Conversion from boolean to a signed:1 bit-field (which only
2727 can hold the values 0 and -1) doesn't lose information - but
2728 it does change the value. */
2729 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2730 warning_at (loc, OPT_Wconversion,
2731 "conversion to %qT from boolean expression", type);
2732 return;
2733
2734 case REAL_CST:
2735 case INTEGER_CST:
7a37fa90 2736 case COMPLEX_CST:
68fca595 2737 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2738 if (conversion_kind == UNSAFE_REAL)
2739 warning_at (loc, OPT_Wfloat_conversion,
2740 "conversion to %qT alters %qT constant value",
2741 type, expr_type);
2742 else if (conversion_kind)
0e3a99ae
AS
2743 warning_at (loc, OPT_Wconversion,
2744 "conversion to %qT alters %qT constant value",
2745 type, expr_type);
2746 return;
2747
2748 case COND_EXPR:
2749 {
3f46d6a5
MLI
2750 /* In case of COND_EXPR, we do not care about the type of
2751 COND_EXPR, only about the conversion of each operand. */
2752 tree op1 = TREE_OPERAND (expr, 1);
2753 tree op2 = TREE_OPERAND (expr, 2);
2754
68fca595
MP
2755 conversion_warning (loc, type, op1);
2756 conversion_warning (loc, type, op2);
3f46d6a5 2757 return;
0e3a99ae
AS
2758 }
2759
2760 default: /* 'expr' is not a constant. */
68fca595 2761 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2762 if (conversion_kind == UNSAFE_REAL)
2763 warning_at (loc, OPT_Wfloat_conversion,
2764 "conversion to %qT from %qT may alter its value",
2765 type, expr_type);
7a37fa90
MM
2766 else if (conversion_kind == UNSAFE_IMAGINARY)
2767 warning_at (loc, OPT_Wconversion,
2768 "conversion to %qT from %qT discards imaginary component",
2769 type, expr_type);
49b0aa18 2770 else if (conversion_kind)
0e3a99ae 2771 warning_at (loc, OPT_Wconversion,
6312e84d
MLI
2772 "conversion to %qT from %qT may alter its value",
2773 type, expr_type);
422c3a54
MLI
2774 }
2775}
2776
07231d4f
MLI
2777/* Produce warnings after a conversion. RESULT is the result of
2778 converting EXPR to TYPE. This is a helper function for
2779 convert_and_check and cp_convert_and_check. */
d74154d5 2780
07231d4f 2781void
68fca595
MP
2782warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2783 tree result)
d74154d5 2784{
68fca595 2785 loc = expansion_point_location_if_in_system_header (loc);
5a3c9cf2 2786
91c41804
RS
2787 if (TREE_CODE (expr) == INTEGER_CST
2788 && (TREE_CODE (type) == INTEGER_TYPE
2789 || TREE_CODE (type) == ENUMERAL_TYPE)
2790 && !int_fits_type_p (expr, type))
2791 {
422c3a54
MLI
2792 /* Do not diagnose overflow in a constant expression merely
2793 because a conversion overflowed. */
91c41804 2794 if (TREE_OVERFLOW (result))
d95787e6
RS
2795 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2796
91c41804 2797 if (TYPE_UNSIGNED (type))
422c3a54 2798 {
91c41804
RS
2799 /* This detects cases like converting -129 or 256 to
2800 unsigned char. */
2801 if (!int_fits_type_p (expr, c_common_signed_type (type)))
5a3c9cf2
PC
2802 warning_at (loc, OPT_Woverflow,
2803 "large integer implicitly truncated to unsigned type");
7060db96 2804 else
68fca595 2805 conversion_warning (loc, type, expr);
91c41804 2806 }
b8698a0f 2807 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
68fca595 2808 warning_at (loc, OPT_Woverflow,
f73fe417
MLI
2809 "overflow in implicit constant conversion");
2810 /* No warning for converting 0x80000000 to int. */
2811 else if (pedantic
2812 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2813 || TYPE_PRECISION (TREE_TYPE (expr))
2814 != TYPE_PRECISION (type)))
5a3c9cf2
PC
2815 warning_at (loc, OPT_Woverflow,
2816 "overflow in implicit constant conversion");
f73fe417 2817
7060db96 2818 else
68fca595 2819 conversion_warning (loc, type, expr);
d74154d5 2820 }
ab22c1fa
CF
2821 else if ((TREE_CODE (result) == INTEGER_CST
2822 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
5a3c9cf2
PC
2823 warning_at (loc, OPT_Woverflow,
2824 "overflow in implicit constant conversion");
7060db96 2825 else
68fca595 2826 conversion_warning (loc, type, expr);
07231d4f
MLI
2827}
2828
2829
2830/* Convert EXPR to TYPE, warning about conversion problems with constants.
2831 Invoke this function on every expression that is converted implicitly,
2832 i.e. because of language rules and not because of an explicit cast. */
2833
2834tree
68fca595 2835convert_and_check (location_t loc, tree type, tree expr)
07231d4f
MLI
2836{
2837 tree result;
8ce94e44
JM
2838 tree expr_for_warning;
2839
2840 /* Convert from a value with possible excess precision rather than
2841 via the semantic type, but do not warn about values not fitting
2842 exactly in the semantic type. */
2843 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2844 {
2845 tree orig_type = TREE_TYPE (expr);
2846 expr = TREE_OPERAND (expr, 0);
2847 expr_for_warning = convert (orig_type, expr);
2848 if (orig_type == type)
2849 return expr_for_warning;
2850 }
2851 else
2852 expr_for_warning = expr;
07231d4f
MLI
2853
2854 if (TREE_TYPE (expr) == type)
2855 return expr;
b8698a0f 2856
07231d4f
MLI
2857 result = convert (type, expr);
2858
7d882b83
ILT
2859 if (c_inhibit_evaluation_warnings == 0
2860 && !TREE_OVERFLOW_P (expr)
2861 && result != error_mark_node)
68fca595 2862 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
07231d4f 2863
91c41804 2864 return result;
96571883
BK
2865}
2866\f
235cfbc4
BS
2867/* A node in a list that describes references to variables (EXPR), which are
2868 either read accesses if WRITER is zero, or write accesses, in which case
2869 WRITER is the parent of EXPR. */
2870struct tlist
2871{
2872 struct tlist *next;
2873 tree expr, writer;
2874};
2875
2876/* Used to implement a cache the results of a call to verify_tree. We only
2877 use this for SAVE_EXPRs. */
2878struct tlist_cache
2879{
2880 struct tlist_cache *next;
2881 struct tlist *cache_before_sp;
2882 struct tlist *cache_after_sp;
2883 tree expr;
2683ed8d
BS
2884};
2885
235cfbc4
BS
2886/* Obstack to use when allocating tlist structures, and corresponding
2887 firstobj. */
2888static struct obstack tlist_obstack;
2889static char *tlist_firstobj = 0;
2890
2891/* Keep track of the identifiers we've warned about, so we can avoid duplicate
2892 warnings. */
2893static struct tlist *warned_ids;
2894/* SAVE_EXPRs need special treatment. We process them only once and then
2895 cache the results. */
2896static struct tlist_cache *save_expr_cache;
2897
35b1a6fa
AJ
2898static void add_tlist (struct tlist **, struct tlist *, tree, int);
2899static void merge_tlist (struct tlist **, struct tlist *, int);
2900static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2901static int warning_candidate_p (tree);
1e4ae551 2902static bool candidate_equal_p (const_tree, const_tree);
35b1a6fa
AJ
2903static void warn_for_collisions (struct tlist *);
2904static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2905static struct tlist *new_tlist (struct tlist *, tree, tree);
2683ed8d 2906
235cfbc4
BS
2907/* Create a new struct tlist and fill in its fields. */
2908static struct tlist *
35b1a6fa 2909new_tlist (struct tlist *next, tree t, tree writer)
235cfbc4
BS
2910{
2911 struct tlist *l;
5d038c4c 2912 l = XOBNEW (&tlist_obstack, struct tlist);
235cfbc4
BS
2913 l->next = next;
2914 l->expr = t;
2915 l->writer = writer;
2916 return l;
2917}
2918
2919/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2920 is nonnull, we ignore any node we find which has a writer equal to it. */
2921
2922static void
35b1a6fa 2923add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
235cfbc4
BS
2924{
2925 while (add)
2926 {
2927 struct tlist *next = add->next;
3f75a254 2928 if (!copy)
235cfbc4 2929 add->next = *to;
1e4ae551 2930 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
235cfbc4
BS
2931 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2932 add = next;
2933 }
2934}
2935
2936/* Merge the nodes of ADD into TO. This merging process is done so that for
2937 each variable that already exists in TO, no new node is added; however if
2938 there is a write access recorded in ADD, and an occurrence on TO is only
2939 a read access, then the occurrence in TO will be modified to record the
2940 write. */
2683ed8d
BS
2941
2942static void
35b1a6fa 2943merge_tlist (struct tlist **to, struct tlist *add, int copy)
235cfbc4
BS
2944{
2945 struct tlist **end = to;
2946
2947 while (*end)
2948 end = &(*end)->next;
2949
2950 while (add)
2951 {
2952 int found = 0;
2953 struct tlist *tmp2;
2954 struct tlist *next = add->next;
2955
2956 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1e4ae551 2957 if (candidate_equal_p (tmp2->expr, add->expr))
235cfbc4
BS
2958 {
2959 found = 1;
3f75a254 2960 if (!tmp2->writer)
235cfbc4
BS
2961 tmp2->writer = add->writer;
2962 }
3f75a254 2963 if (!found)
235cfbc4 2964 {
c2bf53a1 2965 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
235cfbc4
BS
2966 end = &(*end)->next;
2967 *end = 0;
2968 }
2969 add = next;
2970 }
2971}
2972
2973/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2974 references in list LIST conflict with it, excluding reads if ONLY writers
2975 is nonzero. */
2976
2977static void
35b1a6fa
AJ
2978warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2979 int only_writes)
235cfbc4
BS
2980{
2981 struct tlist *tmp;
2982
2983 /* Avoid duplicate warnings. */
2984 for (tmp = warned_ids; tmp; tmp = tmp->next)
1e4ae551 2985 if (candidate_equal_p (tmp->expr, written))
235cfbc4
BS
2986 return;
2987
2988 while (list)
2989 {
1e4ae551
MLI
2990 if (candidate_equal_p (list->expr, written)
2991 && !candidate_equal_p (list->writer, writer)
2992 && (!only_writes || list->writer))
235cfbc4
BS
2993 {
2994 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
8400e75e 2995 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
ca085fd7
MLI
2996 OPT_Wsequence_point, "operation on %qE may be undefined",
2997 list->expr);
235cfbc4
BS
2998 }
2999 list = list->next;
3000 }
3001}
3002
3003/* Given a list LIST of references to variables, find whether any of these
3004 can cause conflicts due to missing sequence points. */
3005
3006static void
35b1a6fa 3007warn_for_collisions (struct tlist *list)
235cfbc4
BS
3008{
3009 struct tlist *tmp;
35b1a6fa 3010
235cfbc4
BS
3011 for (tmp = list; tmp; tmp = tmp->next)
3012 {
3013 if (tmp->writer)
3014 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3015 }
3016}
3017
684d9f3b 3018/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
3019 warnings. */
3020static int
35b1a6fa 3021warning_candidate_p (tree x)
2683ed8d 3022{
07078664
JJ
3023 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3024 return 0;
3025
92e948a8
NF
3026 if (TREE_CODE (x) == BLOCK)
3027 return 0;
3028
07078664 3029 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1e4ae551 3030 (lvalue_p) crash on TRY/CATCH. */
07078664
JJ
3031 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3032 return 0;
3033
3034 if (!lvalue_p (x))
3035 return 0;
3036
3037 /* No point to track non-const calls, they will never satisfy
3038 operand_equal_p. */
3039 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3040 return 0;
3041
3042 if (TREE_CODE (x) == STRING_CST)
3043 return 0;
3044
3045 return 1;
1e4ae551
MLI
3046}
3047
3048/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3049static bool
3050candidate_equal_p (const_tree x, const_tree y)
3051{
3052 return (x == y) || (x && y && operand_equal_p (x, y, 0));
235cfbc4 3053}
2683ed8d 3054
235cfbc4
BS
3055/* Walk the tree X, and record accesses to variables. If X is written by the
3056 parent tree, WRITER is the parent.
3057 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3058 expression or its only operand forces a sequence point, then everything up
3059 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3060 in PNO_SP.
3061 Once we return, we will have emitted warnings if any subexpression before
3062 such a sequence point could be undefined. On a higher level, however, the
3063 sequence point may not be relevant, and we'll merge the two lists.
3064
3065 Example: (b++, a) + b;
3066 The call that processes the COMPOUND_EXPR will store the increment of B
3067 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3068 processes the PLUS_EXPR will need to merge the two lists so that
3069 eventually, all accesses end up on the same list (and we'll warn about the
3070 unordered subexpressions b++ and b.
3071
3072 A note on merging. If we modify the former example so that our expression
3073 becomes
3074 (b++, b) + a
3075 care must be taken not simply to add all three expressions into the final
3076 PNO_SP list. The function merge_tlist takes care of that by merging the
3077 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3078 way, so that no more than one access to B is recorded. */
2683ed8d 3079
235cfbc4 3080static void
35b1a6fa
AJ
3081verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3082 tree writer)
235cfbc4
BS
3083{
3084 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3085 enum tree_code code;
6615c446 3086 enum tree_code_class cl;
2683ed8d 3087
f9e1917e
JM
3088 /* X may be NULL if it is the operand of an empty statement expression
3089 ({ }). */
3090 if (x == NULL)
3091 return;
3092
235cfbc4
BS
3093 restart:
3094 code = TREE_CODE (x);
e3a64162 3095 cl = TREE_CODE_CLASS (code);
2683ed8d 3096
235cfbc4 3097 if (warning_candidate_p (x))
1e4ae551 3098 *pno_sp = new_tlist (*pno_sp, x, writer);
235cfbc4
BS
3099
3100 switch (code)
3101 {
52a84e42 3102 case CONSTRUCTOR:
f7716d57 3103 case SIZEOF_EXPR:
52a84e42
BS
3104 return;
3105
235cfbc4
BS
3106 case COMPOUND_EXPR:
3107 case TRUTH_ANDIF_EXPR:
3108 case TRUTH_ORIF_EXPR:
a25bd9e6 3109 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
235cfbc4
BS
3110 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3111 warn_for_collisions (tmp_nosp);
3112 merge_tlist (pbefore_sp, tmp_before, 0);
3113 merge_tlist (pbefore_sp, tmp_nosp, 0);
a25bd9e6
JM
3114 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
3115 warn_for_collisions (tmp_list2);
235cfbc4 3116 merge_tlist (pbefore_sp, tmp_list3, 0);
a25bd9e6 3117 merge_tlist (pno_sp, tmp_list2, 0);
235cfbc4
BS
3118 return;
3119
3120 case COND_EXPR:
3121 tmp_before = tmp_list2 = 0;
3122 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3123 warn_for_collisions (tmp_list2);
3124 merge_tlist (pbefore_sp, tmp_before, 0);
c2bf53a1 3125 merge_tlist (pbefore_sp, tmp_list2, 0);
235cfbc4
BS
3126
3127 tmp_list3 = tmp_nosp = 0;
3128 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3129 warn_for_collisions (tmp_nosp);
3130 merge_tlist (pbefore_sp, tmp_list3, 0);
3131
3132 tmp_list3 = tmp_list2 = 0;
3133 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3134 warn_for_collisions (tmp_list2);
3135 merge_tlist (pbefore_sp, tmp_list3, 0);
3136 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3137 two first, to avoid warning for (a ? b++ : b++). */
3138 merge_tlist (&tmp_nosp, tmp_list2, 0);
3139 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3140 return;
3141
2683ed8d
BS
3142 case PREDECREMENT_EXPR:
3143 case PREINCREMENT_EXPR:
3144 case POSTDECREMENT_EXPR:
3145 case POSTINCREMENT_EXPR:
235cfbc4
BS
3146 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3147 return;
3148
3149 case MODIFY_EXPR:
3150 tmp_before = tmp_nosp = tmp_list3 = 0;
3151 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3152 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3153 /* Expressions inside the LHS are not ordered wrt. the sequence points
3154 in the RHS. Example:
3155 *a = (a++, 2)
3156 Despite the fact that the modification of "a" is in the before_sp
3157 list (tmp_before), it conflicts with the use of "a" in the LHS.
3158 We can handle this by adding the contents of tmp_list3
3159 to those of tmp_before, and redoing the collision warnings for that
3160 list. */
3161 add_tlist (&tmp_before, tmp_list3, x, 1);
3162 warn_for_collisions (tmp_before);
3163 /* Exclude the LHS itself here; we first have to merge it into the
3164 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3165 didn't exclude the LHS, we'd get it twice, once as a read and once
3166 as a write. */
3167 add_tlist (pno_sp, tmp_list3, x, 0);
3168 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3169
3170 merge_tlist (pbefore_sp, tmp_before, 0);
3171 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3172 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3173 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3174 return;
2683ed8d
BS
3175
3176 case CALL_EXPR:
235cfbc4
BS
3177 /* We need to warn about conflicts among arguments and conflicts between
3178 args and the function address. Side effects of the function address,
3179 however, are not ordered by the sequence point of the call. */
5039610b
SL
3180 {
3181 call_expr_arg_iterator iter;
3182 tree arg;
b8698a0f 3183 tmp_before = tmp_nosp = 0;
5039610b
SL
3184 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3185 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3186 {
3187 tmp_list2 = tmp_list3 = 0;
3188 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3189 merge_tlist (&tmp_list3, tmp_list2, 0);
3190 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3191 }
3192 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3193 warn_for_collisions (tmp_before);
3194 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3195 return;
3196 }
2683ed8d
BS
3197
3198 case TREE_LIST:
3199 /* Scan all the list, e.g. indices of multi dimensional array. */
3200 while (x)
3201 {
235cfbc4
BS
3202 tmp_before = tmp_nosp = 0;
3203 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3204 merge_tlist (&tmp_nosp, tmp_before, 0);
3205 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
3206 x = TREE_CHAIN (x);
3207 }
235cfbc4 3208 return;
2683ed8d 3209
235cfbc4
BS
3210 case SAVE_EXPR:
3211 {
3212 struct tlist_cache *t;
3213 for (t = save_expr_cache; t; t = t->next)
1e4ae551 3214 if (candidate_equal_p (t->expr, x))
235cfbc4 3215 break;
2683ed8d 3216
3f75a254 3217 if (!t)
2683ed8d 3218 {
5d038c4c 3219 t = XOBNEW (&tlist_obstack, struct tlist_cache);
235cfbc4
BS
3220 t->next = save_expr_cache;
3221 t->expr = x;
3222 save_expr_cache = t;
3223
3224 tmp_before = tmp_nosp = 0;
3225 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3226 warn_for_collisions (tmp_nosp);
3227
3228 tmp_list3 = 0;
c2bf53a1 3229 merge_tlist (&tmp_list3, tmp_nosp, 0);
235cfbc4
BS
3230 t->cache_before_sp = tmp_before;
3231 t->cache_after_sp = tmp_list3;
2683ed8d 3232 }
235cfbc4
BS
3233 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3234 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3235 return;
3236 }
2683ed8d 3237
528c22f4
MLI
3238 case ADDR_EXPR:
3239 x = TREE_OPERAND (x, 0);
3240 if (DECL_P (x))
3241 return;
3242 writer = 0;
3243 goto restart;
3244
6615c446
JO
3245 default:
3246 /* For other expressions, simply recurse on their operands.
c22cacf3 3247 Manual tail recursion for unary expressions.
6615c446
JO
3248 Other non-expressions need not be processed. */
3249 if (cl == tcc_unary)
3250 {
6615c446
JO
3251 x = TREE_OPERAND (x, 0);
3252 writer = 0;
3253 goto restart;
3254 }
3255 else if (IS_EXPR_CODE_CLASS (cl))
3256 {
3257 int lp;
5039610b 3258 int max = TREE_OPERAND_LENGTH (x);
6615c446
JO
3259 for (lp = 0; lp < max; lp++)
3260 {
3261 tmp_before = tmp_nosp = 0;
3262 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3263 merge_tlist (&tmp_nosp, tmp_before, 0);
3264 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3265 }
3266 }
3267 return;
2683ed8d 3268 }
2683ed8d
BS
3269}
3270
8d9afc4e 3271/* Try to warn for undefined behavior in EXPR due to missing sequence
2683ed8d
BS
3272 points. */
3273
24e47c76 3274DEBUG_FUNCTION void
35b1a6fa 3275verify_sequence_points (tree expr)
2683ed8d 3276{
235cfbc4 3277 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 3278
235cfbc4
BS
3279 warned_ids = 0;
3280 save_expr_cache = 0;
3281 if (tlist_firstobj == 0)
2683ed8d 3282 {
235cfbc4 3283 gcc_obstack_init (&tlist_obstack);
28dab132 3284 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
3285 }
3286
235cfbc4
BS
3287 verify_tree (expr, &before_sp, &after_sp, 0);
3288 warn_for_collisions (after_sp);
3289 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d 3290}
b30f223b
RS
3291\f
3292/* Validate the expression after `case' and apply default promotions. */
3293
a6c0a76c 3294static tree
62e4eb35 3295check_case_value (location_t loc, tree value)
b30f223b
RS
3296{
3297 if (value == NULL_TREE)
3298 return value;
3299
522ddfa2
JM
3300 if (TREE_CODE (value) == INTEGER_CST)
3301 /* Promote char or short to int. */
3302 value = perform_integral_promotions (value);
3303 else if (value != error_mark_node)
b30f223b 3304 {
62e4eb35 3305 error_at (loc, "case label does not reduce to an integer constant");
b30f223b
RS
3306 value = error_mark_node;
3307 }
b30f223b 3308
bc690db1
RS
3309 constant_expression_warning (value);
3310
b30f223b
RS
3311 return value;
3312}
3313\f
a6c0a76c 3314/* See if the case values LOW and HIGH are in the range of the original
89dbed81 3315 type (i.e. before the default conversion to int) of the switch testing
a6c0a76c
SB
3316 expression.
3317 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2a7e31df 3318 the type before promoting it. CASE_LOW_P is a pointer to the lower
a6c0a76c
SB
3319 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3320 if the case is not a case range.
3321 The caller has to make sure that we are not called with NULL for
b155cfd9
MP
3322 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3323 was a case value that doesn't fit into the range of the ORIG_TYPE.
0fa2e4df 3324 Returns true if the case label is in range of ORIG_TYPE (saturated or
a6c0a76c
SB
3325 untouched) or false if the label is out of range. */
3326
3327static bool
9d548dfb 3328check_case_bounds (location_t loc, tree type, tree orig_type,
b155cfd9
MP
3329 tree *case_low_p, tree *case_high_p,
3330 bool *outside_range_p)
a6c0a76c
SB
3331{
3332 tree min_value, max_value;
3333 tree case_low = *case_low_p;
3334 tree case_high = case_high_p ? *case_high_p : case_low;
3335
3336 /* If there was a problem with the original type, do nothing. */
3337 if (orig_type == error_mark_node)
3338 return true;
3339
3340 min_value = TYPE_MIN_VALUE (orig_type);
3341 max_value = TYPE_MAX_VALUE (orig_type);
3342
0f62c7a0
MP
3343 /* We'll really need integer constants here. */
3344 case_low = fold (case_low);
3345 case_high = fold (case_high);
3346
a6c0a76c
SB
3347 /* Case label is less than minimum for type. */
3348 if (tree_int_cst_compare (case_low, min_value) < 0
3349 && tree_int_cst_compare (case_high, min_value) < 0)
3350 {
9d548dfb
MP
3351 warning_at (loc, 0, "case label value is less than minimum value "
3352 "for type");
b155cfd9 3353 *outside_range_p = true;
a6c0a76c
SB
3354 return false;
3355 }
9f63daea 3356
a6c0a76c
SB
3357 /* Case value is greater than maximum for type. */
3358 if (tree_int_cst_compare (case_low, max_value) > 0
3359 && tree_int_cst_compare (case_high, max_value) > 0)
3360 {
9d548dfb 3361 warning_at (loc, 0, "case label value exceeds maximum value for type");
b155cfd9 3362 *outside_range_p = true;
a6c0a76c
SB
3363 return false;
3364 }
3365
3366 /* Saturate lower case label value to minimum. */
3367 if (tree_int_cst_compare (case_high, min_value) >= 0
3368 && tree_int_cst_compare (case_low, min_value) < 0)
3369 {
9d548dfb
MP
3370 warning_at (loc, 0, "lower value in case label range"
3371 " less than minimum value for type");
b155cfd9 3372 *outside_range_p = true;
a6c0a76c
SB
3373 case_low = min_value;
3374 }
9f63daea 3375
a6c0a76c
SB
3376 /* Saturate upper case label value to maximum. */
3377 if (tree_int_cst_compare (case_low, max_value) <= 0
3378 && tree_int_cst_compare (case_high, max_value) > 0)
3379 {
9d548dfb
MP
3380 warning_at (loc, 0, "upper value in case label range"
3381 " exceeds maximum value for type");
b155cfd9 3382 *outside_range_p = true;
a6c0a76c
SB
3383 case_high = max_value;
3384 }
3385
3386 if (*case_low_p != case_low)
3387 *case_low_p = convert (type, case_low);
3388 if (case_high_p && *case_high_p != case_high)
3389 *case_high_p = convert (type, case_high);
3390
3391 return true;
3392}
3393\f
b30f223b
RS
3394/* Return an integer type with BITS bits of precision,
3395 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3396
3397tree
35b1a6fa 3398c_common_type_for_size (unsigned int bits, int unsignedp)
b30f223b 3399{
78a7c317
DD
3400 int i;
3401
a311b52c
JM
3402 if (bits == TYPE_PRECISION (integer_type_node))
3403 return unsignedp ? unsigned_type_node : integer_type_node;
3404
3fc7e390 3405 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
3406 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3407
3fc7e390 3408 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
3409 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3410
3fc7e390 3411 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
3412 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3413
3fc7e390 3414 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
3415 return (unsignedp ? long_long_unsigned_type_node
3416 : long_long_integer_type_node);
3417
78a7c317
DD
3418 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3419 if (int_n_enabled_p[i]
3420 && bits == int_n_data[i].bitsize)
3421 return (unsignedp ? int_n_trees[i].unsigned_type
3422 : int_n_trees[i].signed_type);
a6766312 3423
835f9b4d
GRK
3424 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3425 return (unsignedp ? widest_unsigned_literal_type_node
3426 : widest_integer_literal_type_node);
3427
3fc7e390
RS
3428 if (bits <= TYPE_PRECISION (intQI_type_node))
3429 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3430
3431 if (bits <= TYPE_PRECISION (intHI_type_node))
3432 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3433
3434 if (bits <= TYPE_PRECISION (intSI_type_node))
3435 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3436
3437 if (bits <= TYPE_PRECISION (intDI_type_node))
3438 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3439
b30f223b
RS
3440 return 0;
3441}
3442
ab22c1fa
CF
3443/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3444 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3445 and saturating if SATP is nonzero, otherwise not saturating. */
3446
3447tree
3448c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3449 int unsignedp, int satp)
3450{
ef4bddc2 3451 machine_mode mode;
ab22c1fa
CF
3452 if (ibit == 0)
3453 mode = unsignedp ? UQQmode : QQmode;
3454 else
3455 mode = unsignedp ? UHAmode : HAmode;
3456
3457 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3458 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3459 break;
3460
3461 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3462 {
3463 sorry ("GCC cannot support operators with integer types and "
3464 "fixed-point types that have too many integral and "
3465 "fractional bits together");
3466 return 0;
3467 }
3468
3469 return c_common_type_for_mode (mode, satp);
3470}
3471
d1d3865f
ZW
3472/* Used for communication between c_common_type_for_mode and
3473 c_register_builtin_type. */
793c625f 3474tree registered_builtin_types;
d1d3865f 3475
b30f223b
RS
3476/* Return a data type that has machine mode MODE.
3477 If the mode is an integer,
ab22c1fa
CF
3478 then UNSIGNEDP selects between signed and unsigned types.
3479 If the mode is a fixed-point mode,
3480 then UNSIGNEDP selects between saturating and nonsaturating types. */
b30f223b
RS
3481
3482tree
ef4bddc2 3483c_common_type_for_mode (machine_mode mode, int unsignedp)
b30f223b 3484{
d1d3865f 3485 tree t;
78a7c317 3486 int i;
d1d3865f 3487
a311b52c
JM
3488 if (mode == TYPE_MODE (integer_type_node))
3489 return unsignedp ? unsigned_type_node : integer_type_node;
3490
b30f223b
RS
3491 if (mode == TYPE_MODE (signed_char_type_node))
3492 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3493
3494 if (mode == TYPE_MODE (short_integer_type_node))
3495 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3496
b30f223b
RS
3497 if (mode == TYPE_MODE (long_integer_type_node))
3498 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3499
3500 if (mode == TYPE_MODE (long_long_integer_type_node))
3501 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3502
78a7c317
DD
3503 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3504 if (int_n_enabled_p[i]
3505 && mode == int_n_data[i].m)
3506 return (unsignedp ? int_n_trees[i].unsigned_type
3507 : int_n_trees[i].signed_type);
a6766312 3508
835f9b4d 3509 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 3510 return unsignedp ? widest_unsigned_literal_type_node
6de9cd9a 3511 : widest_integer_literal_type_node;
835f9b4d 3512
0afeef64 3513 if (mode == QImode)
3fc7e390
RS
3514 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3515
0afeef64 3516 if (mode == HImode)
3fc7e390
RS
3517 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3518
0afeef64 3519 if (mode == SImode)
3fc7e390
RS
3520 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3521
0afeef64 3522 if (mode == DImode)
3fc7e390
RS
3523 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3524
21a9616b 3525#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
3526 if (mode == TYPE_MODE (intTI_type_node))
3527 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 3528#endif
a6d7e156 3529
b30f223b
RS
3530 if (mode == TYPE_MODE (float_type_node))
3531 return float_type_node;
3532
3533 if (mode == TYPE_MODE (double_type_node))
3534 return double_type_node;
3535
3536 if (mode == TYPE_MODE (long_double_type_node))
3537 return long_double_type_node;
3538
c65699ef
JM
3539 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3540 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
3541 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
3542 return FLOATN_NX_TYPE_NODE (i);
3543
ff42324e
NS
3544 if (mode == TYPE_MODE (void_type_node))
3545 return void_type_node;
9f63daea 3546
b30f223b 3547 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
19b3ffbc
DD
3548 return (unsignedp
3549 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3550 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b
RS
3551
3552 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
19b3ffbc
DD
3553 return (unsignedp
3554 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3555 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b 3556
7e7e470f
RH
3557 if (COMPLEX_MODE_P (mode))
3558 {
ef4bddc2 3559 machine_mode inner_mode;
7e7e470f
RH
3560 tree inner_type;
3561
3562 if (mode == TYPE_MODE (complex_float_type_node))
3563 return complex_float_type_node;
3564 if (mode == TYPE_MODE (complex_double_type_node))
3565 return complex_double_type_node;
3566 if (mode == TYPE_MODE (complex_long_double_type_node))
3567 return complex_long_double_type_node;
3568
c65699ef
JM
3569 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3570 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
3571 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
3572 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
3573
7e7e470f
RH
3574 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3575 return complex_integer_type_node;
3576
3577 inner_mode = GET_MODE_INNER (mode);
3578 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3579 if (inner_type != NULL_TREE)
3580 return build_complex_type (inner_type);
3581 }
3582 else if (VECTOR_MODE_P (mode))
4a5eab38 3583 {
ef4bddc2 3584 machine_mode inner_mode = GET_MODE_INNER (mode);
4a5eab38
PB
3585 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3586 if (inner_type != NULL_TREE)
3587 return build_vector_type_for_mode (inner_type, mode);
0afeef64 3588 }
4061f623 3589
9a8ce21f
JG
3590 if (mode == TYPE_MODE (dfloat32_type_node))
3591 return dfloat32_type_node;
3592 if (mode == TYPE_MODE (dfloat64_type_node))
3593 return dfloat64_type_node;
3594 if (mode == TYPE_MODE (dfloat128_type_node))
3595 return dfloat128_type_node;
3596
ab22c1fa
CF
3597 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3598 {
3599 if (mode == TYPE_MODE (short_fract_type_node))
3600 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3601 if (mode == TYPE_MODE (fract_type_node))
3602 return unsignedp ? sat_fract_type_node : fract_type_node;
3603 if (mode == TYPE_MODE (long_fract_type_node))
3604 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3605 if (mode == TYPE_MODE (long_long_fract_type_node))
3606 return unsignedp ? sat_long_long_fract_type_node
3607 : long_long_fract_type_node;
3608
3609 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3610 return unsignedp ? sat_unsigned_short_fract_type_node
3611 : unsigned_short_fract_type_node;
3612 if (mode == TYPE_MODE (unsigned_fract_type_node))
3613 return unsignedp ? sat_unsigned_fract_type_node
3614 : unsigned_fract_type_node;
3615 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3616 return unsignedp ? sat_unsigned_long_fract_type_node
3617 : unsigned_long_fract_type_node;
3618 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3619 return unsignedp ? sat_unsigned_long_long_fract_type_node
3620 : unsigned_long_long_fract_type_node;
3621
3622 if (mode == TYPE_MODE (short_accum_type_node))
3623 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3624 if (mode == TYPE_MODE (accum_type_node))
3625 return unsignedp ? sat_accum_type_node : accum_type_node;
3626 if (mode == TYPE_MODE (long_accum_type_node))
3627 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3628 if (mode == TYPE_MODE (long_long_accum_type_node))
3629 return unsignedp ? sat_long_long_accum_type_node
3630 : long_long_accum_type_node;
3631
3632 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3633 return unsignedp ? sat_unsigned_short_accum_type_node
3634 : unsigned_short_accum_type_node;
3635 if (mode == TYPE_MODE (unsigned_accum_type_node))
3636 return unsignedp ? sat_unsigned_accum_type_node
3637 : unsigned_accum_type_node;
3638 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3639 return unsignedp ? sat_unsigned_long_accum_type_node
3640 : unsigned_long_accum_type_node;
3641 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3642 return unsignedp ? sat_unsigned_long_long_accum_type_node
3643 : unsigned_long_long_accum_type_node;
3644
3645 if (mode == QQmode)
3646 return unsignedp ? sat_qq_type_node : qq_type_node;
3647 if (mode == HQmode)
3648 return unsignedp ? sat_hq_type_node : hq_type_node;
3649 if (mode == SQmode)
3650 return unsignedp ? sat_sq_type_node : sq_type_node;
3651 if (mode == DQmode)
3652 return unsignedp ? sat_dq_type_node : dq_type_node;
3653 if (mode == TQmode)
3654 return unsignedp ? sat_tq_type_node : tq_type_node;
3655
3656 if (mode == UQQmode)
3657 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3658 if (mode == UHQmode)
3659 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3660 if (mode == USQmode)
3661 return unsignedp ? sat_usq_type_node : usq_type_node;
3662 if (mode == UDQmode)
3663 return unsignedp ? sat_udq_type_node : udq_type_node;
3664 if (mode == UTQmode)
3665 return unsignedp ? sat_utq_type_node : utq_type_node;
3666
3667 if (mode == HAmode)
3668 return unsignedp ? sat_ha_type_node : ha_type_node;
3669 if (mode == SAmode)
3670 return unsignedp ? sat_sa_type_node : sa_type_node;
3671 if (mode == DAmode)
3672 return unsignedp ? sat_da_type_node : da_type_node;
3673 if (mode == TAmode)
3674 return unsignedp ? sat_ta_type_node : ta_type_node;
3675
3676 if (mode == UHAmode)
3677 return unsignedp ? sat_uha_type_node : uha_type_node;
3678 if (mode == USAmode)
3679 return unsignedp ? sat_usa_type_node : usa_type_node;
3680 if (mode == UDAmode)
3681 return unsignedp ? sat_uda_type_node : uda_type_node;
3682 if (mode == UTAmode)
3683 return unsignedp ? sat_uta_type_node : uta_type_node;
3684 }
3685
d1d3865f 3686 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
7a421706
MS
3687 if (TYPE_MODE (TREE_VALUE (t)) == mode
3688 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
d1d3865f
ZW
3689 return TREE_VALUE (t);
3690
b30f223b
RS
3691 return 0;
3692}
693a6128 3693
12753674
RE
3694tree
3695c_common_unsigned_type (tree type)
3696{
3697 return c_common_signed_or_unsigned_type (1, type);
3698}
3699
693a6128
GRK
3700/* Return a signed type the same as TYPE in other respects. */
3701
3702tree
35b1a6fa 3703c_common_signed_type (tree type)
693a6128 3704{
ceef8ce4 3705 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
3706}
3707
3708/* Return a type the same as TYPE except unsigned or
3709 signed according to UNSIGNEDP. */
3710
3711tree
35b1a6fa 3712c_common_signed_or_unsigned_type (int unsignedp, tree type)
693a6128 3713{
c74a03d2 3714 tree type1;
78a7c317 3715 int i;
693a6128 3716
c74a03d2
RAE
3717 /* This block of code emulates the behavior of the old
3718 c_common_unsigned_type. In particular, it returns
3719 long_unsigned_type_node if passed a long, even when a int would
3720 have the same size. This is necessary for warnings to work
3721 correctly in archs where sizeof(int) == sizeof(long) */
3722
3723 type1 = TYPE_MAIN_VARIANT (type);
3724 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3725 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3726 if (type1 == integer_type_node || type1 == unsigned_type_node)
3727 return unsignedp ? unsigned_type_node : integer_type_node;
3728 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3729 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3730 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3731 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3732 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3733 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
78a7c317
DD
3734
3735 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3736 if (int_n_enabled_p[i]
3737 && (type1 == int_n_trees[i].unsigned_type
3738 || type1 == int_n_trees[i].signed_type))
3739 return (unsignedp ? int_n_trees[i].unsigned_type
3740 : int_n_trees[i].signed_type);
3741
c74a03d2
RAE
3742 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3743 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3744#if HOST_BITS_PER_WIDE_INT >= 64
3745 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3746 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3747#endif
3748 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3749 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3750 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3751 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3752 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3753 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3754 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3755 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3756
70d3fcab
AH
3757#define C_COMMON_FIXED_TYPES(NAME) \
3758 if (type1 == short_ ## NAME ## _type_node \
3759 || type1 == unsigned_short_ ## NAME ## _type_node) \
3760 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3761 : short_ ## NAME ## _type_node; \
3762 if (type1 == NAME ## _type_node \
3763 || type1 == unsigned_ ## NAME ## _type_node) \
3764 return unsignedp ? unsigned_ ## NAME ## _type_node \
3765 : NAME ## _type_node; \
3766 if (type1 == long_ ## NAME ## _type_node \
3767 || type1 == unsigned_long_ ## NAME ## _type_node) \
3768 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3769 : long_ ## NAME ## _type_node; \
3770 if (type1 == long_long_ ## NAME ## _type_node \
3771 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3772 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3773 : long_long_ ## NAME ## _type_node;
3774
3775#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3776 if (type1 == NAME ## _type_node \
3777 || type1 == u ## NAME ## _type_node) \
3778 return unsignedp ? u ## NAME ## _type_node \
3779 : NAME ## _type_node;
3780
3781#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3782 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3783 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3784 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3785 : sat_ ## short_ ## NAME ## _type_node; \
3786 if (type1 == sat_ ## NAME ## _type_node \
3787 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3788 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3789 : sat_ ## NAME ## _type_node; \
3790 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3791 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3792 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3793 : sat_ ## long_ ## NAME ## _type_node; \
3794 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3795 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3796 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3797 : sat_ ## long_long_ ## NAME ## _type_node;
3798
3799#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3800 if (type1 == sat_ ## NAME ## _type_node \
3801 || type1 == sat_ ## u ## NAME ## _type_node) \
3802 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3803 : sat_ ## NAME ## _type_node;
3804
3805 C_COMMON_FIXED_TYPES (fract);
3806 C_COMMON_FIXED_TYPES_SAT (fract);
3807 C_COMMON_FIXED_TYPES (accum);
3808 C_COMMON_FIXED_TYPES_SAT (accum);
3809
3810 C_COMMON_FIXED_MODE_TYPES (qq);
3811 C_COMMON_FIXED_MODE_TYPES (hq);
3812 C_COMMON_FIXED_MODE_TYPES (sq);
3813 C_COMMON_FIXED_MODE_TYPES (dq);
3814 C_COMMON_FIXED_MODE_TYPES (tq);
3815 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3816 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3817 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3818 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3819 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3820 C_COMMON_FIXED_MODE_TYPES (ha);
3821 C_COMMON_FIXED_MODE_TYPES (sa);
3822 C_COMMON_FIXED_MODE_TYPES (da);
3823 C_COMMON_FIXED_MODE_TYPES (ta);
3824 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3825 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3826 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3827 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
ab22c1fa 3828
bc15d0ef
JM
3829 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3830 the precision; they have precision set to match their range, but
3831 may use a wider mode to match an ABI. If we change modes, we may
3832 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3833 the precision as well, so as to yield correct results for
3834 bit-field types. C++ does not have these separate bit-field
3835 types, and producing a signed or unsigned variant of an
3836 ENUMERAL_TYPE may cause other problems as well. */
3837
1e204133
RAE
3838 if (!INTEGRAL_TYPE_P (type)
3839 || TYPE_UNSIGNED (type) == unsignedp)
3840 return type;
3841
bc15d0ef
JM
3842#define TYPE_OK(node) \
3843 (TYPE_MODE (type) == TYPE_MODE (node) \
41b81065 3844 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
bc15d0ef 3845 if (TYPE_OK (signed_char_type_node))
693a6128 3846 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
bc15d0ef 3847 if (TYPE_OK (integer_type_node))
693a6128 3848 return unsignedp ? unsigned_type_node : integer_type_node;
bc15d0ef 3849 if (TYPE_OK (short_integer_type_node))
693a6128 3850 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
bc15d0ef 3851 if (TYPE_OK (long_integer_type_node))
693a6128 3852 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
bc15d0ef 3853 if (TYPE_OK (long_long_integer_type_node))
693a6128
GRK
3854 return (unsignedp ? long_long_unsigned_type_node
3855 : long_long_integer_type_node);
78a7c317
DD
3856
3857 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3858 if (int_n_enabled_p[i]
3859 && TYPE_MODE (type) == int_n_data[i].m
3860 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3861 return (unsignedp ? int_n_trees[i].unsigned_type
3862 : int_n_trees[i].signed_type);
3863
bc15d0ef 3864 if (TYPE_OK (widest_integer_literal_type_node))
693a6128
GRK
3865 return (unsignedp ? widest_unsigned_literal_type_node
3866 : widest_integer_literal_type_node);
4a063bec
RH
3867
3868#if HOST_BITS_PER_WIDE_INT >= 64
bc15d0ef 3869 if (TYPE_OK (intTI_type_node))
4a063bec
RH
3870 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3871#endif
bc15d0ef 3872 if (TYPE_OK (intDI_type_node))
4a063bec 3873 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
bc15d0ef 3874 if (TYPE_OK (intSI_type_node))
4a063bec 3875 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
bc15d0ef 3876 if (TYPE_OK (intHI_type_node))
4a063bec 3877 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
bc15d0ef 3878 if (TYPE_OK (intQI_type_node))
4a063bec 3879 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
bc15d0ef 3880#undef TYPE_OK
4a063bec 3881
41b81065 3882 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
693a6128 3883}
9649812a 3884
38a4afee
MM
3885/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3886
3887tree
3888c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3889{
78a7c317
DD
3890 int i;
3891
38a4afee
MM
3892 /* Extended integer types of the same width as a standard type have
3893 lesser rank, so those of the same width as int promote to int or
3894 unsigned int and are valid for printf formats expecting int or
3895 unsigned int. To avoid such special cases, avoid creating
3896 extended integer types for bit-fields if a standard integer type
3897 is available. */
3898 if (width == TYPE_PRECISION (integer_type_node))
3899 return unsignedp ? unsigned_type_node : integer_type_node;
3900 if (width == TYPE_PRECISION (signed_char_type_node))
3901 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3902 if (width == TYPE_PRECISION (short_integer_type_node))
3903 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3904 if (width == TYPE_PRECISION (long_integer_type_node))
3905 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3906 if (width == TYPE_PRECISION (long_long_integer_type_node))
3907 return (unsignedp ? long_long_unsigned_type_node
3908 : long_long_integer_type_node);
78a7c317
DD
3909 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3910 if (int_n_enabled_p[i]
3911 && width == int_n_data[i].bitsize)
3912 return (unsignedp ? int_n_trees[i].unsigned_type
3913 : int_n_trees[i].signed_type);
38a4afee
MM
3914 return build_nonstandard_integer_type (width, unsignedp);
3915}
3916
9649812a
MM
3917/* The C version of the register_builtin_type langhook. */
3918
3919void
3920c_register_builtin_type (tree type, const char* name)
3921{
3922 tree decl;
3923
c2255bc4
AH
3924 decl = build_decl (UNKNOWN_LOCATION,
3925 TYPE_DECL, get_identifier (name), type);
9649812a
MM
3926 DECL_ARTIFICIAL (decl) = 1;
3927 if (!TYPE_NAME (type))
3928 TYPE_NAME (type) = decl;
3929 pushdecl (decl);
d1d3865f
ZW
3930
3931 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
9649812a 3932}
6acfe908 3933\f
78ef5b89 3934/* Print an error message for invalid operands to arith operation
ba47d38d 3935 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
745e411d
DM
3936 RICHLOC is a rich location for the message, containing either
3937 three separate locations for each of the operator and operands
3938
3939 lhs op rhs
3940 ~~~ ^~ ~~~
3941
3942 (C FE), or one location ranging over all over them
3943
3944 lhs op rhs
3945 ~~~~^~~~~~
3946
3947 (C++ FE). */
b30f223b
RS
3948
3949void
745e411d 3950binary_op_error (rich_location *richloc, enum tree_code code,
ba47d38d 3951 tree type0, tree type1)
b30f223b 3952{
b3694847 3953 const char *opname;
89c78d7d 3954
b30f223b
RS
3955 switch (code)
3956 {
b30f223b
RS
3957 case PLUS_EXPR:
3958 opname = "+"; break;
3959 case MINUS_EXPR:
3960 opname = "-"; break;
3961 case MULT_EXPR:
3962 opname = "*"; break;
3963 case MAX_EXPR:
3964 opname = "max"; break;
3965 case MIN_EXPR:
3966 opname = "min"; break;
3967 case EQ_EXPR:
3968 opname = "=="; break;
3969 case NE_EXPR:
3970 opname = "!="; break;
3971 case LE_EXPR:
3972 opname = "<="; break;
3973 case GE_EXPR:
3974 opname = ">="; break;
3975 case LT_EXPR:
3976 opname = "<"; break;
3977 case GT_EXPR:
3978 opname = ">"; break;
3979 case LSHIFT_EXPR:
3980 opname = "<<"; break;
3981 case RSHIFT_EXPR:
3982 opname = ">>"; break;
3983 case TRUNC_MOD_EXPR:
047de90b 3984 case FLOOR_MOD_EXPR:
b30f223b
RS
3985 opname = "%"; break;
3986 case TRUNC_DIV_EXPR:
047de90b 3987 case FLOOR_DIV_EXPR:
b30f223b
RS
3988 opname = "/"; break;
3989 case BIT_AND_EXPR:
3990 opname = "&"; break;
3991 case BIT_IOR_EXPR:
3992 opname = "|"; break;
3993 case TRUTH_ANDIF_EXPR:
3994 opname = "&&"; break;
3995 case TRUTH_ORIF_EXPR:
3996 opname = "||"; break;
3997 case BIT_XOR_EXPR:
3998 opname = "^"; break;
6d819282 3999 default:
37b2f290 4000 gcc_unreachable ();
b30f223b 4001 }
745e411d
DM
4002 error_at_rich_loc (richloc,
4003 "invalid operands to binary %s (have %qT and %qT)",
4004 opname, type0, type1);
b30f223b
RS
4005}
4006\f
50f305ca
MLI
4007/* Given an expression as a tree, return its original type. Do this
4008 by stripping any conversion that preserves the sign and precision. */
4009static tree
4010expr_original_type (tree expr)
4011{
4012 STRIP_SIGN_NOPS (expr);
4013 return TREE_TYPE (expr);
4014}
4015
b30f223b
RS
4016/* Subroutine of build_binary_op, used for comparison operations.
4017 See if the operands have both been converted from subword integer types
4018 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
4019 This function is also responsible for converting the two operands
4020 to the proper common type for comparison.
b30f223b
RS
4021
4022 The arguments of this function are all pointers to local variables
4023 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4024 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4025
393e8e8b
MP
4026 LOC is the location of the comparison.
4027
b30f223b
RS
4028 If this function returns nonzero, it means that the comparison has
4029 a constant value. What this function returns is an expression for
4030 that value. */
4031
4032tree
393e8e8b
MP
4033shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4034 tree *restype_ptr, enum tree_code *rescode_ptr)
b30f223b 4035{
b3694847 4036 tree type;
b30f223b
RS
4037 tree op0 = *op0_ptr;
4038 tree op1 = *op1_ptr;
4039 int unsignedp0, unsignedp1;
4040 int real1, real2;
4041 tree primop0, primop1;
4042 enum tree_code code = *rescode_ptr;
4043
4044 /* Throw away any conversions to wider types
4045 already present in the operands. */
4046
828fb3ba
JM
4047 primop0 = c_common_get_narrower (op0, &unsignedp0);
4048 primop1 = c_common_get_narrower (op1, &unsignedp1);
b30f223b 4049
126e6609
JJ
4050 /* If primopN is first sign-extended from primopN's precision to opN's
4051 precision, then zero-extended from opN's precision to
4052 *restype_ptr precision, shortenings might be invalid. */
4053 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4054 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4055 && !unsignedp0
4056 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4057 primop0 = op0;
4058 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4059 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4060 && !unsignedp1
4061 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4062 primop1 = op1;
4063
b30f223b
RS
4064 /* Handle the case that OP0 does not *contain* a conversion
4065 but it *requires* conversion to FINAL_TYPE. */
4066
4067 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
8df83eae 4068 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b30f223b 4069 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
8df83eae 4070 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b30f223b
RS
4071
4072 /* If one of the operands must be floated, we cannot optimize. */
4073 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4074 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4075
4076 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
4077 so value is preserved), for canonicalization. Don't do this if
4078 the second arg is 0. */
b30f223b 4079
5af6001b 4080 if (TREE_CONSTANT (primop0)
ab22c1fa
CF
4081 && !integer_zerop (primop1) && !real_zerop (primop1)
4082 && !fixed_zerop (primop1))
b30f223b 4083 {
fab27f52
MM
4084 std::swap (primop0, primop1);
4085 std::swap (op0, op1);
b30f223b
RS
4086 *op0_ptr = op0;
4087 *op1_ptr = op1;
fab27f52
MM
4088 std::swap (unsignedp0, unsignedp1);
4089 std::swap (real1, real2);
b30f223b
RS
4090
4091 switch (code)
4092 {
4093 case LT_EXPR:
4094 code = GT_EXPR;
4095 break;
4096 case GT_EXPR:
4097 code = LT_EXPR;
4098 break;
4099 case LE_EXPR:
4100 code = GE_EXPR;
4101 break;
4102 case GE_EXPR:
4103 code = LE_EXPR;
4104 break;
6d819282
MK
4105 default:
4106 break;
b30f223b
RS
4107 }
4108 *rescode_ptr = code;
4109 }
4110
4111 /* If comparing an integer against a constant more bits wide,
4112 maybe we can deduce a value of 1 or 0 independent of the data.
4113 Or else truncate the constant now
4114 rather than extend the variable at run time.
4115
4116 This is only interesting if the constant is the wider arg.
4117 Also, it is not safe if the constant is unsigned and the
4118 variable arg is signed, since in this case the variable
4119 would be sign-extended and then regarded as unsigned.
4120 Our technique fails in this case because the lowest/highest
4121 possible unsigned results don't follow naturally from the
4122 lowest/highest possible values of the variable operand.
4123 For just EQ_EXPR and NE_EXPR there is another technique that
4124 could be used: see if the constant can be faithfully represented
4125 in the other operand's type, by truncating it and reextending it
4126 and see if that preserves the constant's value. */
4127
4128 if (!real1 && !real2
ab22c1fa 4129 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b30f223b
RS
4130 && TREE_CODE (primop1) == INTEGER_CST
4131 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4132 {
4133 int min_gt, max_gt, min_lt, max_lt;
4134 tree maxval, minval;
4135 /* 1 if comparison is nominally unsigned. */
8df83eae 4136 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b30f223b
RS
4137 tree val;
4138
ceef8ce4
NB
4139 type = c_common_signed_or_unsigned_type (unsignedp0,
4140 TREE_TYPE (primop0));
8bbd5685 4141
b30f223b
RS
4142 maxval = TYPE_MAX_VALUE (type);
4143 minval = TYPE_MIN_VALUE (type);
4144
4145 if (unsignedp && !unsignedp0)
ceef8ce4 4146 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
4147
4148 if (TREE_TYPE (primop1) != *restype_ptr)
fae1b38d 4149 {
af9c6659
NS
4150 /* Convert primop1 to target type, but do not introduce
4151 additional overflow. We know primop1 is an int_cst. */
807e902e 4152 primop1 = force_fit_type (*restype_ptr,
697e0b28
RS
4153 wi::to_wide
4154 (primop1,
4155 TYPE_PRECISION (*restype_ptr)),
807e902e 4156 0, TREE_OVERFLOW (primop1));
fae1b38d 4157 }
b30f223b
RS
4158 if (type != *restype_ptr)
4159 {
4160 minval = convert (*restype_ptr, minval);
4161 maxval = convert (*restype_ptr, maxval);
4162 }
4163
807e902e
KZ
4164 min_gt = tree_int_cst_lt (primop1, minval);
4165 max_gt = tree_int_cst_lt (primop1, maxval);
4166 min_lt = tree_int_cst_lt (minval, primop1);
4167 max_lt = tree_int_cst_lt (maxval, primop1);
b30f223b
RS
4168
4169 val = 0;
4170 /* This used to be a switch, but Genix compiler can't handle that. */
4171 if (code == NE_EXPR)
4172 {
4173 if (max_lt || min_gt)
de7df9eb 4174 val = truthvalue_true_node;
b30f223b
RS
4175 }
4176 else if (code == EQ_EXPR)
4177 {
4178 if (max_lt || min_gt)
de7df9eb 4179 val = truthvalue_false_node;
b30f223b
RS
4180 }
4181 else if (code == LT_EXPR)
4182 {
4183 if (max_lt)
de7df9eb 4184 val = truthvalue_true_node;
b30f223b 4185 if (!min_lt)
de7df9eb 4186 val = truthvalue_false_node;
b30f223b
RS
4187 }
4188 else if (code == GT_EXPR)
4189 {
4190 if (min_gt)
de7df9eb 4191 val = truthvalue_true_node;
b30f223b 4192 if (!max_gt)
de7df9eb 4193 val = truthvalue_false_node;
b30f223b
RS
4194 }
4195 else if (code == LE_EXPR)
4196 {
4197 if (!max_gt)
de7df9eb 4198 val = truthvalue_true_node;
b30f223b 4199 if (min_gt)
de7df9eb 4200 val = truthvalue_false_node;
b30f223b
RS
4201 }
4202 else if (code == GE_EXPR)
4203 {
4204 if (!min_lt)
de7df9eb 4205 val = truthvalue_true_node;
b30f223b 4206 if (max_lt)
de7df9eb 4207 val = truthvalue_false_node;
b30f223b
RS
4208 }
4209
4210 /* If primop0 was sign-extended and unsigned comparison specd,
4211 we did a signed comparison above using the signed type bounds.
4212 But the comparison we output must be unsigned.
4213
4214 Also, for inequalities, VAL is no good; but if the signed
4215 comparison had *any* fixed result, it follows that the
4216 unsigned comparison just tests the sign in reverse
4217 (positive values are LE, negative ones GE).
4218 So we can generate an unsigned comparison
4219 against an extreme value of the signed type. */
4220
4221 if (unsignedp && !unsignedp0)
4222 {
4223 if (val != 0)
4224 switch (code)
4225 {
4226 case LT_EXPR:
4227 case GE_EXPR:
4228 primop1 = TYPE_MIN_VALUE (type);
4229 val = 0;
4230 break;
4231
4232 case LE_EXPR:
4233 case GT_EXPR:
4234 primop1 = TYPE_MAX_VALUE (type);
4235 val = 0;
4236 break;
6d819282
MK
4237
4238 default:
4239 break;
b30f223b 4240 }
12753674 4241 type = c_common_unsigned_type (type);
b30f223b
RS
4242 }
4243
d0eccfcd 4244 if (TREE_CODE (primop0) != INTEGER_CST
269adb9d
JM
4245 /* Don't warn if it's from a (non-system) macro. */
4246 && !(from_macro_expansion_at
4247 (expansion_point_location_if_in_system_header
4248 (EXPR_LOCATION (primop0)))))
b30f223b 4249 {
de7df9eb 4250 if (val == truthvalue_false_node)
50f305ca
MLI
4251 warning_at (loc, OPT_Wtype_limits,
4252 "comparison is always false due to limited range of data type");
de7df9eb 4253 if (val == truthvalue_true_node)
50f305ca
MLI
4254 warning_at (loc, OPT_Wtype_limits,
4255 "comparison is always true due to limited range of data type");
b30f223b
RS
4256 }
4257
4258 if (val != 0)
4259 {
4260 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4261 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3 4262 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b30f223b
RS
4263 return val;
4264 }
4265
4266 /* Value is not predetermined, but do the comparison
4267 in the type of the operand that is not constant.
4268 TYPE is already properly set. */
4269 }
9a8ce21f
JG
4270
4271 /* If either arg is decimal float and the other is float, find the
4272 proper common type to use for comparison. */
6f450181
RB
4273 else if (real1 && real2
4274 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4275 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4276 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4277
4278 /* If either arg is decimal float and the other is float, fail. */
9a8ce21f
JG
4279 else if (real1 && real2
4280 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4281 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
6f450181 4282 return 0;
9a8ce21f 4283
b30f223b 4284 else if (real1 && real2
766f6c30
RS
4285 && (TYPE_PRECISION (TREE_TYPE (primop0))
4286 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
4287 type = TREE_TYPE (primop0);
4288
4289 /* If args' natural types are both narrower than nominal type
4290 and both extend in the same manner, compare them
4291 in the type of the wider arg.
4292 Otherwise must actually extend both to the nominal
4293 common type lest different ways of extending
4294 alter the result.
4295 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4296
4297 else if (unsignedp0 == unsignedp1 && real1 == real2
4298 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4299 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4300 {
4301 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4 4302 type = c_common_signed_or_unsigned_type (unsignedp0
8df83eae 4303 || TYPE_UNSIGNED (*restype_ptr),
ceef8ce4 4304 type);
b30f223b
RS
4305 /* Make sure shorter operand is extended the right way
4306 to match the longer operand. */
ceef8ce4
NB
4307 primop0
4308 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4309 TREE_TYPE (primop0)),
4310 primop0);
4311 primop1
4312 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4313 TREE_TYPE (primop1)),
4314 primop1);
b30f223b
RS
4315 }
4316 else
4317 {
4318 /* Here we must do the comparison on the nominal type
4319 using the args exactly as we received them. */
4320 type = *restype_ptr;
4321 primop0 = op0;
4322 primop1 = op1;
4323
4324 if (!real1 && !real2 && integer_zerop (primop1)
8df83eae 4325 && TYPE_UNSIGNED (*restype_ptr))
b30f223b
RS
4326 {
4327 tree value = 0;
50f305ca
MLI
4328 /* All unsigned values are >= 0, so we warn. However,
4329 if OP0 is a constant that is >= 0, the signedness of
4330 the comparison isn't an issue, so suppress the
4331 warning. */
4332 bool warn =
8400e75e 4333 warn_type_limits && !in_system_header_at (loc)
50f305ca
MLI
4334 && !(TREE_CODE (primop0) == INTEGER_CST
4335 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4336 primop0)))
4337 /* Do not warn for enumeration types. */
4338 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4339
b30f223b
RS
4340 switch (code)
4341 {
4342 case GE_EXPR:
50f305ca
MLI
4343 if (warn)
4344 warning_at (loc, OPT_Wtype_limits,
4345 "comparison of unsigned expression >= 0 is always true");
de7df9eb 4346 value = truthvalue_true_node;
b30f223b
RS
4347 break;
4348
4349 case LT_EXPR:
50f305ca
MLI
4350 if (warn)
4351 warning_at (loc, OPT_Wtype_limits,
4352 "comparison of unsigned expression < 0 is always false");
de7df9eb 4353 value = truthvalue_false_node;
6d819282
MK
4354 break;
4355
4356 default:
4357 break;
b30f223b
RS
4358 }
4359
4360 if (value != 0)
4361 {
4362 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4363 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3
RS
4364 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4365 primop0, value);
b30f223b
RS
4366 return value;
4367 }
4368 }
4369 }
4370
4371 *op0_ptr = convert (type, primop0);
4372 *op1_ptr = convert (type, primop1);
4373
de7df9eb 4374 *restype_ptr = truthvalue_type_node;
b30f223b
RS
4375
4376 return 0;
4377}
4378\f
7552da58
JJ
4379/* Return a tree for the sum or difference (RESULTCODE says which)
4380 of pointer PTROP and integer INTOP. */
4381
4382tree
db3927fb 4383pointer_int_sum (location_t loc, enum tree_code resultcode,
fd9b0f32 4384 tree ptrop, tree intop, bool complain)
7552da58 4385{
6ac01510 4386 tree size_exp, ret;
7552da58 4387
7552da58 4388 /* The result is a pointer of the same type that is being added. */
7552da58
JJ
4389 tree result_type = TREE_TYPE (ptrop);
4390
4391 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4392 {
fd9b0f32
PC
4393 if (complain && warn_pointer_arith)
4394 pedwarn (loc, OPT_Wpointer_arith,
4395 "pointer of type %<void *%> used in arithmetic");
4396 else if (!complain)
4397 return error_mark_node;
7552da58
JJ
4398 size_exp = integer_one_node;
4399 }
4400 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4401 {
fd9b0f32
PC
4402 if (complain && warn_pointer_arith)
4403 pedwarn (loc, OPT_Wpointer_arith,
4404 "pointer to a function used in arithmetic");
4405 else if (!complain)
4406 return error_mark_node;
7552da58
JJ
4407 size_exp = integer_one_node;
4408 }
7552da58 4409 else
4f2e1536 4410 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
7552da58 4411
6ac01510
ILT
4412 /* We are manipulating pointer values, so we don't need to warn
4413 about relying on undefined signed overflow. We disable the
4414 warning here because we use integer types so fold won't know that
4415 they are really pointers. */
4416 fold_defer_overflow_warnings ();
4417
7552da58
JJ
4418 /* If what we are about to multiply by the size of the elements
4419 contains a constant term, apply distributive law
4420 and multiply that constant term separately.
4421 This helps produce common subexpressions. */
7552da58 4422 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3f75a254 4423 && !TREE_CONSTANT (intop)
7552da58
JJ
4424 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4425 && TREE_CONSTANT (size_exp)
4426 /* If the constant comes from pointer subtraction,
4427 skip this optimization--it would cause an error. */
4428 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4429 /* If the constant is unsigned, and smaller than the pointer size,
4430 then we must skip this optimization. This is because it could cause
4431 an overflow error if the constant is negative but INTOP is not. */
3f75a254 4432 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
7552da58
JJ
4433 || (TYPE_PRECISION (TREE_TYPE (intop))
4434 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4435 {
4436 enum tree_code subcode = resultcode;
4437 tree int_type = TREE_TYPE (intop);
4438 if (TREE_CODE (intop) == MINUS_EXPR)
4439 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4440 /* Convert both subexpression types to the type of intop,
4441 because weird cases involving pointer arithmetic
4442 can result in a sum or difference with different type args. */
ba47d38d
AH
4443 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4444 subcode, ptrop,
7552da58
JJ
4445 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4446 intop = convert (int_type, TREE_OPERAND (intop, 0));
4447 }
4448
4449 /* Convert the integer argument to a type the same size as sizetype
4450 so the multiply won't overflow spuriously. */
7552da58 4451 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
8df83eae 4452 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
35b1a6fa 4453 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
8df83eae 4454 TYPE_UNSIGNED (sizetype)), intop);
7552da58
JJ
4455
4456 /* Replace the integer argument with a suitable product by the object size.
9e9ef331 4457 Do this multiplication as signed, then convert to the appropriate type
65de6659 4458 for the pointer operation and disregard an overflow that occurred only
9e9ef331
EB
4459 because of the sign-extension change in the latter conversion. */
4460 {
d90ec4f2
JM
4461 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4462 convert (TREE_TYPE (intop), size_exp));
9e9ef331
EB
4463 intop = convert (sizetype, t);
4464 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
807e902e 4465 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
9e9ef331 4466 }
5be014d5 4467
280f1ffa 4468 /* Create the sum or difference. */
5be014d5 4469 if (resultcode == MINUS_EXPR)
db3927fb 4470 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
7552da58 4471
5d49b6a7 4472 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
6ac01510
ILT
4473
4474 fold_undefer_and_ignore_overflow_warnings ();
4475
4476 return ret;
7552da58
JJ
4477}
4478\f
e5a94231
JM
4479/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4480 and if NON_CONST is known not to be permitted in an evaluated part
4481 of a constant expression. */
4482
4483tree
4484c_wrap_maybe_const (tree expr, bool non_const)
4485{
4486 bool nowarning = TREE_NO_WARNING (expr);
4487 location_t loc = EXPR_LOCATION (expr);
4488
4489 /* This should never be called for C++. */
4490 if (c_dialect_cxx ())
4491 gcc_unreachable ();
4492
4493 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4494 STRIP_TYPE_NOPS (expr);
4495 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4496 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4497 if (nowarning)
4498 TREE_NO_WARNING (expr) = 1;
4499 protected_set_expr_location (expr, loc);
4500
4501 return expr;
4502}
4503
928c19bb
JM
4504/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4505 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4506 around the SAVE_EXPR if needed so that c_fully_fold does not need
4507 to look inside SAVE_EXPRs. */
4508
4509tree
4510c_save_expr (tree expr)
4511{
4512 bool maybe_const = true;
4513 if (c_dialect_cxx ())
4514 return save_expr (expr);
4515 expr = c_fully_fold (expr, false, &maybe_const);
4516 expr = save_expr (expr);
4517 if (!maybe_const)
e5a94231 4518 expr = c_wrap_maybe_const (expr, true);
928c19bb
JM
4519 return expr;
4520}
4521
b3c6d2ea
ILT
4522/* Return whether EXPR is a declaration whose address can never be
4523 NULL. */
4524
4525bool
58f9752a 4526decl_with_nonnull_addr_p (const_tree expr)
b3c6d2ea
ILT
4527{
4528 return (DECL_P (expr)
4529 && (TREE_CODE (expr) == PARM_DECL
4530 || TREE_CODE (expr) == LABEL_DECL
4531 || !DECL_WEAK (expr)));
4532}
4533
b30f223b 4534/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
85498824
JM
4535 or for an `if' or `while' statement or ?..: exp. It should already
4536 have been validated to be of suitable type; otherwise, a bad
4537 diagnostic may result.
b30f223b 4538
ba47d38d
AH
4539 The EXPR is located at LOCATION.
4540
b30f223b
RS
4541 This preparation consists of taking the ordinary
4542 representation of an expression expr and producing a valid tree
4543 boolean expression describing whether expr is nonzero. We could
de7df9eb 4544 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b30f223b
RS
4545 but we optimize comparisons, &&, ||, and !.
4546
de7df9eb 4547 The resulting type should always be `truthvalue_type_node'. */
b30f223b
RS
4548
4549tree
ba47d38d 4550c_common_truthvalue_conversion (location_t location, tree expr)
b30f223b 4551{
b30f223b
RS
4552 switch (TREE_CODE (expr))
4553 {
d1a7edaf 4554 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
6f312d18
ZW
4555 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4556 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4557 case ORDERED_EXPR: case UNORDERED_EXPR:
90ec750d
RS
4558 if (TREE_TYPE (expr) == truthvalue_type_node)
4559 return expr;
c2255bc4 4560 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
90ec750d 4561 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
c2255bc4 4562 goto ret;
90ec750d 4563
b30f223b
RS
4564 case TRUTH_ANDIF_EXPR:
4565 case TRUTH_ORIF_EXPR:
4566 case TRUTH_AND_EXPR:
4567 case TRUTH_OR_EXPR:
9379fac9 4568 case TRUTH_XOR_EXPR:
90ec750d
RS
4569 if (TREE_TYPE (expr) == truthvalue_type_node)
4570 return expr;
c2255bc4 4571 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
b8698a0f 4572 c_common_truthvalue_conversion (location,
c2255bc4
AH
4573 TREE_OPERAND (expr, 0)),
4574 c_common_truthvalue_conversion (location,
4575 TREE_OPERAND (expr, 1)));
4576 goto ret;
18c0f675 4577
18d00205 4578 case TRUTH_NOT_EXPR:
90ec750d
RS
4579 if (TREE_TYPE (expr) == truthvalue_type_node)
4580 return expr;
c2255bc4
AH
4581 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4582 c_common_truthvalue_conversion (location,
4583 TREE_OPERAND (expr, 0)));
4584 goto ret;
18d00205 4585
b30f223b
RS
4586 case ERROR_MARK:
4587 return expr;
4588
4589 case INTEGER_CST:
d95787e6
RS
4590 return integer_zerop (expr) ? truthvalue_false_node
4591 : truthvalue_true_node;
b30f223b
RS
4592
4593 case REAL_CST:
010c4d9c
RS
4594 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4595 ? truthvalue_true_node
4596 : truthvalue_false_node;
b30f223b 4597
ab22c1fa
CF
4598 case FIXED_CST:
4599 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4600 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4601 ? truthvalue_true_node
4602 : truthvalue_false_node;
4603
90ec750d 4604 case FUNCTION_DECL:
e51fbec3 4605 expr = build_unary_op (location, ADDR_EXPR, expr, false);
90ec750d
RS
4606 /* Fall through. */
4607
b30f223b 4608 case ADDR_EXPR:
1998463c 4609 {
f6f08360 4610 tree inner = TREE_OPERAND (expr, 0);
b3c6d2ea 4611 if (decl_with_nonnull_addr_p (inner))
1998463c 4612 {
b3c6d2ea 4613 /* Common Ada/Pascal programmer's mistake. */
ba47d38d
AH
4614 warning_at (location,
4615 OPT_Waddress,
4616 "the address of %qD will always evaluate as %<true%>",
4617 inner);
1998463c
SB
4618 return truthvalue_true_node;
4619 }
33766b66 4620 break;
1998463c 4621 }
b30f223b 4622
766f6c30 4623 case COMPLEX_EXPR:
c2255bc4 4624 expr = build_binary_op (EXPR_LOCATION (expr),
ba47d38d 4625 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 4626 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
ba47d38d
AH
4627 c_common_truthvalue_conversion (location,
4628 TREE_OPERAND (expr, 0)),
4629 c_common_truthvalue_conversion (location,
4630 TREE_OPERAND (expr, 1)),
766f6c30 4631 0);
c2255bc4 4632 goto ret;
766f6c30 4633
b30f223b
RS
4634 case NEGATE_EXPR:
4635 case ABS_EXPR:
4636 case FLOAT_EXPR:
8ce94e44 4637 case EXCESS_PRECISION_EXPR:
da7d8304 4638 /* These don't change whether an object is nonzero or zero. */
ba47d38d 4639 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b30f223b
RS
4640
4641 case LROTATE_EXPR:
4642 case RROTATE_EXPR:
da7d8304 4643 /* These don't change whether an object is zero or nonzero, but
b30f223b
RS
4644 we can't ignore them if their second arg has side-effects. */
4645 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
c2255bc4
AH
4646 {
4647 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4648 TREE_OPERAND (expr, 1),
b8698a0f 4649 c_common_truthvalue_conversion
c2255bc4
AH
4650 (location, TREE_OPERAND (expr, 0)));
4651 goto ret;
4652 }
b30f223b 4653 else
ba47d38d
AH
4654 return c_common_truthvalue_conversion (location,
4655 TREE_OPERAND (expr, 0));
b57062ca 4656
b30f223b 4657 case COND_EXPR:
a09e9e35
BE
4658 if (warn_int_in_bool_context
4659 && !from_macro_definition_at (EXPR_LOCATION (expr)))
144a96e4
BE
4660 {
4661 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
4662 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
4663 if (TREE_CODE (val1) == INTEGER_CST
4664 && TREE_CODE (val2) == INTEGER_CST
4665 && !integer_zerop (val1)
4666 && !integer_zerop (val2)
4667 && (!integer_onep (val1)
4668 || !integer_onep (val2)))
4669 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
a09e9e35
BE
4670 "?: using integer constants in boolean context, "
4671 "the expression will always evaluate to %<true%>");
144a96e4 4672 }
b30f223b 4673 /* Distribute the conversion into the arms of a COND_EXPR. */
928c19bb 4674 if (c_dialect_cxx ())
c2255bc4 4675 {
4cc4f2f4
JJ
4676 tree op1 = TREE_OPERAND (expr, 1);
4677 tree op2 = TREE_OPERAND (expr, 2);
4678 /* In C++ one of the arms might have void type if it is throw. */
4679 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4680 op1 = c_common_truthvalue_conversion (location, op1);
4681 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4682 op2 = c_common_truthvalue_conversion (location, op2);
db3927fb 4683 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4cc4f2f4 4684 TREE_OPERAND (expr, 0), op1, op2);
c2255bc4
AH
4685 goto ret;
4686 }
928c19bb 4687 else
c2255bc4
AH
4688 {
4689 /* Folding will happen later for C. */
4690 expr = build3 (COND_EXPR, truthvalue_type_node,
4691 TREE_OPERAND (expr, 0),
4692 c_common_truthvalue_conversion (location,
4693 TREE_OPERAND (expr, 1)),
4694 c_common_truthvalue_conversion (location,
4695 TREE_OPERAND (expr, 2)));
4696 goto ret;
4697 }
b30f223b 4698
1043771b 4699 CASE_CONVERT:
1ee44b26
JM
4700 {
4701 tree totype = TREE_TYPE (expr);
4702 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4703
076fecad 4704 if (POINTER_TYPE_P (totype)
451dcc66 4705 && !c_inhibit_evaluation_warnings
076fecad
PP
4706 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4707 {
4708 tree inner = expr;
4709 STRIP_NOPS (inner);
4710
4711 if (DECL_P (inner))
4712 warning_at (location,
4713 OPT_Waddress,
4714 "the compiler can assume that the address of "
4715 "%qD will always evaluate to %<true%>",
4716 inner);
4717 }
4718
1ee44b26
JM
4719 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4720 since that affects how `default_conversion' will behave. */
4721 if (TREE_CODE (totype) == REFERENCE_TYPE
4722 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4723 break;
4724 /* Don't strip a conversion from C++0x scoped enum, since they
4725 don't implicitly convert to other types. */
4726 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4727 && ENUM_IS_SCOPED (fromtype))
4728 break;
4729 /* If this isn't narrowing the argument, we can ignore it. */
4730 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4731 return c_common_truthvalue_conversion (location,
4732 TREE_OPERAND (expr, 0));
4733 }
b30f223b
RS
4734 break;
4735
e2aab13d 4736 case MODIFY_EXPR:
fbc8d2d3
ILT
4737 if (!TREE_NO_WARNING (expr)
4738 && warn_parentheses)
4739 {
638fc14f
MP
4740 warning_at (location, OPT_Wparentheses,
4741 "suggest parentheses around assignment used as "
4742 "truth value");
fbc8d2d3
ILT
4743 TREE_NO_WARNING (expr) = 1;
4744 }
e2aab13d 4745 break;
b57062ca 4746
6d819282
MK
4747 default:
4748 break;
b30f223b
RS
4749 }
4750
f0b996c5 4751 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 4752 {
5386338c 4753 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
c2255bc4 4754 expr = (build_binary_op
ba47d38d
AH
4755 (EXPR_LOCATION (expr),
4756 (TREE_SIDE_EFFECTS (expr)
f0b8d9aa 4757 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
c9f9eb5d
AH
4758 c_common_truthvalue_conversion
4759 (location,
e51fbec3 4760 build_unary_op (location, REALPART_EXPR, t, false)),
c9f9eb5d
AH
4761 c_common_truthvalue_conversion
4762 (location,
e51fbec3 4763 build_unary_op (location, IMAGPART_EXPR, t, false)),
f0b8d9aa 4764 0));
c2255bc4 4765 goto ret;
f0b8d9aa 4766 }
f0b996c5 4767
ab22c1fa
CF
4768 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4769 {
4770 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4771 FCONST0 (TYPE_MODE
4772 (TREE_TYPE (expr))));
ca80e52b 4773 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
ab22c1fa 4774 }
c2255bc4
AH
4775 else
4776 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
ab22c1fa 4777
c2255bc4
AH
4778 ret:
4779 protected_set_expr_location (expr, location);
4780 return expr;
b30f223b
RS
4781}
4782\f
9bc15050
RG
4783static void def_builtin_1 (enum built_in_function fncode,
4784 const char *name,
4785 enum built_in_class fnclass,
4786 tree fntype, tree libtype,
4787 bool both_p, bool fallback_p, bool nonansi_p,
4788 tree fnattrs, bool implicit_p);
fc2aaf30 4789
3932261a
MM
4790
4791/* Apply the TYPE_QUALS to the new DECL. */
4792
4793void
35b1a6fa 4794c_apply_type_quals_to_decl (int type_quals, tree decl)
3932261a 4795{
4b011bbf 4796 tree type = TREE_TYPE (decl);
9f63daea 4797
5a6159dd
AP
4798 if (type == error_mark_node)
4799 return;
4b011bbf 4800
329af3c7
JM
4801 if ((type_quals & TYPE_QUAL_CONST)
4802 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4803 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4804 constructor can produce constant init, so rely on cp_finish_decl to
4805 clear TREE_READONLY if the variable has non-constant init. */
3932261a
MM
4806 TREE_READONLY (decl) = 1;
4807 if (type_quals & TYPE_QUAL_VOLATILE)
4808 {
4809 TREE_SIDE_EFFECTS (decl) = 1;
4810 TREE_THIS_VOLATILE (decl) = 1;
4811 }
6946bc60 4812 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 4813 {
4b011bbf
JM
4814 while (type && TREE_CODE (type) == ARRAY_TYPE)
4815 /* Allow 'restrict' on arrays of pointers.
4816 FIXME currently we just ignore it. */
4817 type = TREE_TYPE (type);
4818 if (!type
4819 || !POINTER_TYPE_P (type)
4820 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
bda67431 4821 error ("invalid use of %<restrict%>");
3932261a
MM
4822 }
4823}
4824
ca752f39 4825struct c_type_hasher : ggc_ptr_hash<tree_node>
2a22f99c
TS
4826{
4827 static hashval_t hash (tree);
4828 static bool equal (tree, tree);
4829};
4830
6de9cd9a
DN
4831/* Hash function for the problem of multiple type definitions in
4832 different files. This must hash all types that will compare
4833 equal via comptypes to the same value. In practice it hashes
9cf737f8 4834 on some of the simple stuff and leaves the details to comptypes. */
6de9cd9a 4835
2a22f99c
TS
4836hashval_t
4837c_type_hasher::hash (tree t)
6de9cd9a 4838{
a19e4d44 4839 int n_elements;
6de9cd9a 4840 int shift, size;
6de9cd9a
DN
4841 tree t2;
4842 switch (TREE_CODE (t))
4843 {
8c27b7d4 4844 /* For pointers, hash on pointee type plus some swizzling. */
325c3691 4845 case POINTER_TYPE:
2a22f99c 4846 return hash (TREE_TYPE (t)) ^ 0x3003003;
325c3691
RH
4847 /* Hash on number of elements and total size. */
4848 case ENUMERAL_TYPE:
4849 shift = 3;
4850 t2 = TYPE_VALUES (t);
4851 break;
4852 case RECORD_TYPE:
4853 shift = 0;
4854 t2 = TYPE_FIELDS (t);
4855 break;
4856 case QUAL_UNION_TYPE:
4857 shift = 1;
4858 t2 = TYPE_FIELDS (t);
4859 break;
4860 case UNION_TYPE:
4861 shift = 2;
4862 t2 = TYPE_FIELDS (t);
4863 break;
4864 default:
366de0ce 4865 gcc_unreachable ();
6de9cd9a 4866 }
a19e4d44
NF
4867 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4868 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4869 n_elements = list_length (t2);
6fc3c3c0
TT
4870 /* We might have a VLA here. */
4871 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4872 size = 0;
4873 else
4874 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
a19e4d44 4875 return ((size << 24) | (n_elements << shift));
6de9cd9a
DN
4876}
4877
2a22f99c
TS
4878bool
4879c_type_hasher::equal (tree t1, tree t2)
4880{
4881 return lang_hooks.types_compatible_p (t1, t2);
4882}
4883
4884static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4fe52ce9 4885
41472af8 4886/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 4887 or a type. Return -1 if we don't do anything special. */
41472af8 4888
4862826d 4889alias_set_type
35b1a6fa 4890c_common_get_alias_set (tree t)
41472af8 4891{
cb9c2485
JM
4892 /* For VLAs, use the alias set of the element type rather than the
4893 default of alias set 0 for types compared structurally. */
4894 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4895 {
4896 if (TREE_CODE (t) == ARRAY_TYPE)
4897 return get_alias_set (TREE_TYPE (t));
4898 return -1;
4899 }
4900
74d86f4f 4901 /* That's all the expressions we handle specially. */
3f75a254 4902 if (!TYPE_P (t))
74d86f4f
RH
4903 return -1;
4904
95bd1dd7 4905 /* The C standard guarantees that any object may be accessed via an
74d86f4f
RH
4906 lvalue that has character type. */
4907 if (t == char_type_node
4908 || t == signed_char_type_node
4909 || t == unsigned_char_type_node)
3bdf5ad1 4910 return 0;
3932261a 4911
f824e5c3
RK
4912 /* The C standard specifically allows aliasing between signed and
4913 unsigned variants of the same type. We treat the signed
4914 variant as canonical. */
8df83eae 4915 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
8f215dce 4916 {
ceef8ce4 4917 tree t1 = c_common_signed_type (t);
f824e5c3 4918
8f215dce
JJ
4919 /* t1 == t can happen for boolean nodes which are always unsigned. */
4920 if (t1 != t)
4921 return get_alias_set (t1);
4922 }
ece32014 4923
6de9cd9a
DN
4924 /* Handle the case of multiple type nodes referring to "the same" type,
4925 which occurs with IMA. These share an alias set. FIXME: Currently only
4926 C90 is handled. (In C99 type compatibility is not transitive, which
4927 complicates things mightily. The alias set splay trees can theoretically
4928 represent this, but insertion is tricky when you consider all the
4929 different orders things might arrive in.) */
4930
4931 if (c_language != clk_c || flag_isoc99)
4932 return -1;
4933
9cf737f8 4934 /* Save time if there's only one input file. */
d974312d 4935 if (num_in_fnames == 1)
6de9cd9a
DN
4936 return -1;
4937
4938 /* Pointers need special handling if they point to any type that
4939 needs special handling (below). */
4940 if (TREE_CODE (t) == POINTER_TYPE)
4941 {
4942 tree t2;
4943 /* Find bottom type under any nested POINTERs. */
9f63daea 4944 for (t2 = TREE_TYPE (t);
762f7d9d
TT
4945 TREE_CODE (t2) == POINTER_TYPE;
4946 t2 = TREE_TYPE (t2))
4947 ;
a868811e
MP
4948 if (!RECORD_OR_UNION_TYPE_P (t2)
4949 && TREE_CODE (t2) != ENUMERAL_TYPE)
762f7d9d 4950 return -1;
6de9cd9a 4951 if (TYPE_SIZE (t2) == 0)
762f7d9d 4952 return -1;
6de9cd9a
DN
4953 }
4954 /* These are the only cases that need special handling. */
a868811e 4955 if (!RECORD_OR_UNION_TYPE_P (t)
6de9cd9a 4956 && TREE_CODE (t) != ENUMERAL_TYPE
6de9cd9a
DN
4957 && TREE_CODE (t) != POINTER_TYPE)
4958 return -1;
4959 /* Undefined? */
4960 if (TYPE_SIZE (t) == 0)
4961 return -1;
4962
9f63daea 4963 /* Look up t in hash table. Only one of the compatible types within each
6de9cd9a
DN
4964 alias set is recorded in the table. */
4965 if (!type_hash_table)
2a22f99c
TS
4966 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4967 tree *slot = type_hash_table->find_slot (t, INSERT);
6de9cd9a 4968 if (*slot != NULL)
6a3203c8
AP
4969 {
4970 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4971 return TYPE_ALIAS_SET ((tree)*slot);
4972 }
6de9cd9a
DN
4973 else
4974 /* Our caller will assign and record (in t) a new alias set; all we need
4975 to do is remember t in the hash table. */
4976 *slot = t;
4977
3bdf5ad1 4978 return -1;
41472af8 4979}
0213a355 4980\f
c2255bc4 4981/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
296674db 4982 the IS_SIZEOF parameter indicates which operator is being applied.
c2255bc4
AH
4983 The COMPLAIN flag controls whether we should diagnose possibly
4984 ill-formed constructs or not. LOC is the location of the SIZEOF or
296674db
JM
4985 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4986 a type in any context should be returned, rather than the normal
4987 alignment for that type. */
03a08664 4988
0213a355 4989tree
c2255bc4 4990c_sizeof_or_alignof_type (location_t loc,
296674db
JM
4991 tree type, bool is_sizeof, bool min_alignof,
4992 int complain)
0213a355 4993{
fa72b064
GDR
4994 const char *op_name;
4995 tree value = NULL;
4996 enum tree_code type_code = TREE_CODE (type);
35b1a6fa 4997
03a08664 4998 op_name = is_sizeof ? "sizeof" : "__alignof__";
35b1a6fa 4999
fa72b064 5000 if (type_code == FUNCTION_TYPE)
0213a355 5001 {
03a08664 5002 if (is_sizeof)
fa72b064 5003 {
44d90fe1
PC
5004 if (complain && warn_pointer_arith)
5005 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 5006 "invalid application of %<sizeof%> to a function type");
5ade1ed2
DG
5007 else if (!complain)
5008 return error_mark_node;
fa72b064
GDR
5009 value = size_one_node;
5010 }
5011 else
d19fa6b5
JM
5012 {
5013 if (complain)
5014 {
5015 if (c_dialect_cxx ())
c1771a20 5016 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
d19fa6b5
JM
5017 "%<alignof%> applied to a function type");
5018 else
c1771a20 5019 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
d19fa6b5
JM
5020 "%<_Alignof%> applied to a function type");
5021 }
5022 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5023 }
fa72b064
GDR
5024 }
5025 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5026 {
35b1a6fa 5027 if (type_code == VOID_TYPE
44d90fe1
PC
5028 && complain && warn_pointer_arith)
5029 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 5030 "invalid application of %qs to a void type", op_name);
5ade1ed2
DG
5031 else if (!complain)
5032 return error_mark_node;
fa72b064 5033 value = size_one_node;
0213a355 5034 }
73ac190a
PC
5035 else if (!COMPLETE_TYPE_P (type)
5036 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
0213a355 5037 {
ea793912 5038 if (complain)
73ac190a 5039 error_at (loc, "invalid application of %qs to incomplete type %qT",
c2255bc4 5040 op_name, type);
cb6addf4 5041 return error_mark_node;
0213a355 5042 }
73ac190a
PC
5043 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5044 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5045 {
5046 if (complain)
5047 error_at (loc, "invalid application of %qs to array type %qT of "
5048 "incomplete element type", op_name, type);
5049 return error_mark_node;
5050 }
0213a355 5051 else
fa72b064 5052 {
03a08664 5053 if (is_sizeof)
fa72b064 5054 /* Convert in case a char is more than one unit. */
db3927fb
AH
5055 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5056 size_int (TYPE_PRECISION (char_type_node)
5057 / BITS_PER_UNIT));
296674db 5058 else if (min_alignof)
2793eeab 5059 value = size_int (min_align_of_type (type));
fa72b064 5060 else
a4e9ffe5 5061 value = size_int (TYPE_ALIGN_UNIT (type));
fa72b064 5062 }
0213a355 5063
3ac8781c
RG
5064 /* VALUE will have the middle-end integer type sizetype.
5065 However, we should really return a value of type `size_t',
5066 which is just a typedef for an ordinary integer type. */
db3927fb 5067 value = fold_convert_loc (loc, size_type_node, value);
35b1a6fa 5068
fa72b064 5069 return value;
0213a355
JM
5070}
5071
5072/* Implement the __alignof keyword: Return the minimum required
837edd5f
GK
5073 alignment of EXPR, measured in bytes. For VAR_DECLs,
5074 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
c2255bc4
AH
5075 from an "aligned" __attribute__ specification). LOC is the
5076 location of the ALIGNOF operator. */
7f4edbcb 5077
0213a355 5078tree
c2255bc4 5079c_alignof_expr (location_t loc, tree expr)
0213a355
JM
5080{
5081 tree t;
5082
837edd5f 5083 if (VAR_OR_FUNCTION_DECL_P (expr))
a4e9ffe5 5084 t = size_int (DECL_ALIGN_UNIT (expr));
35b1a6fa 5085
0213a355
JM
5086 else if (TREE_CODE (expr) == COMPONENT_REF
5087 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5088 {
c2255bc4 5089 error_at (loc, "%<__alignof%> applied to a bit-field");
0213a355
JM
5090 t = size_one_node;
5091 }
5092 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 5093 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
a4e9ffe5 5094 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
35b1a6fa 5095
22d03525 5096 else if (INDIRECT_REF_P (expr))
0213a355
JM
5097 {
5098 tree t = TREE_OPERAND (expr, 0);
5099 tree best = t;
5100 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
35b1a6fa 5101
1043771b 5102 while (CONVERT_EXPR_P (t)
173bf5be 5103 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
5104 {
5105 int thisalign;
5106
5107 t = TREE_OPERAND (t, 0);
5108 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5109 if (thisalign > bestalign)
5110 best = t, bestalign = thisalign;
5111 }
c2255bc4 5112 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
0213a355
JM
5113 }
5114 else
c2255bc4 5115 return c_alignof (loc, TREE_TYPE (expr));
0213a355 5116
db3927fb 5117 return fold_convert_loc (loc, size_type_node, t);
0213a355
JM
5118}
5119\f
df061a43
RS
5120/* Handle C and C++ default attributes. */
5121
5122enum built_in_attribute
5123{
5124#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5125#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
e384e6b5 5126#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
df061a43
RS
5127#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5128#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
df061a43
RS
5129#include "builtin-attrs.def"
5130#undef DEF_ATTR_NULL_TREE
5131#undef DEF_ATTR_INT
e384e6b5 5132#undef DEF_ATTR_STRING
df061a43
RS
5133#undef DEF_ATTR_IDENT
5134#undef DEF_ATTR_TREE_LIST
df061a43
RS
5135 ATTR_LAST
5136};
5137
5138static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5139
35b1a6fa 5140static void c_init_attributes (void);
df061a43 5141
a0274e3e 5142enum c_builtin_type
7f4edbcb 5143{
10841285
MM
5144#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5145#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5146#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5147#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5148#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5149#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
10a0d495 5150#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
f6a7cffc
TS
5151#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5152 ARG6) NAME,
5153#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5154 ARG6, ARG7) NAME,
5155#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5156 ARG6, ARG7, ARG8) NAME,
d9a6bd32
JJ
5157#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5158 ARG6, ARG7, ARG8, ARG9) NAME,
5159#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5160 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5161#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5162 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
10841285
MM
5163#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5164#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5165#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
08291658 5166#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
10a0d495 5167#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
f6a7cffc 5168#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
56a9f6bc 5169 NAME,
3e32ee19
NS
5170#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5171 ARG6) NAME,
56a9f6bc
TS
5172#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5173 ARG6, ARG7) NAME,
10841285
MM
5174#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5175#include "builtin-types.def"
5176#undef DEF_PRIMITIVE_TYPE
5177#undef DEF_FUNCTION_TYPE_0
5178#undef DEF_FUNCTION_TYPE_1
5179#undef DEF_FUNCTION_TYPE_2
5180#undef DEF_FUNCTION_TYPE_3
5181#undef DEF_FUNCTION_TYPE_4
10a0d495
JJ
5182#undef DEF_FUNCTION_TYPE_5
5183#undef DEF_FUNCTION_TYPE_6
a0274e3e 5184#undef DEF_FUNCTION_TYPE_7
acf0174b 5185#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5186#undef DEF_FUNCTION_TYPE_9
5187#undef DEF_FUNCTION_TYPE_10
5188#undef DEF_FUNCTION_TYPE_11
10841285
MM
5189#undef DEF_FUNCTION_TYPE_VAR_0
5190#undef DEF_FUNCTION_TYPE_VAR_1
5191#undef DEF_FUNCTION_TYPE_VAR_2
08291658 5192#undef DEF_FUNCTION_TYPE_VAR_3
10a0d495
JJ
5193#undef DEF_FUNCTION_TYPE_VAR_4
5194#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5195#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5196#undef DEF_FUNCTION_TYPE_VAR_7
10841285 5197#undef DEF_POINTER_TYPE
a0274e3e
JJ
5198 BT_LAST
5199};
5200
5201typedef enum c_builtin_type builtin_type;
10841285 5202
a0274e3e
JJ
5203/* A temporary array for c_common_nodes_and_builtins. Used in
5204 communication with def_fn_type. */
5205static tree builtin_types[(int) BT_LAST + 1];
10841285 5206
a0274e3e
JJ
5207/* A helper function for c_common_nodes_and_builtins. Build function type
5208 for DEF with return type RET and N arguments. If VAR is true, then the
5209 function should be variadic after those N arguments.
5210
5211 Takes special care not to ICE if any of the types involved are
5212 error_mark_node, which indicates that said type is not in fact available
5213 (see builtin_type_for_size). In which case the function type as a whole
5214 should be error_mark_node. */
5215
5216static void
5217def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5218{
8242dd04
NF
5219 tree t;
5220 tree *args = XALLOCAVEC (tree, n);
a0274e3e
JJ
5221 va_list list;
5222 int i;
5223
5224 va_start (list, n);
5225 for (i = 0; i < n; ++i)
5226 {
d75d71e0 5227 builtin_type a = (builtin_type) va_arg (list, int);
a0274e3e
JJ
5228 t = builtin_types[a];
5229 if (t == error_mark_node)
5230 goto egress;
8242dd04 5231 args[i] = t;
a0274e3e 5232 }
a0274e3e 5233
a0274e3e
JJ
5234 t = builtin_types[ret];
5235 if (t == error_mark_node)
5236 goto egress;
8242dd04
NF
5237 if (var)
5238 t = build_varargs_function_type_array (t, n, args);
5239 else
5240 t = build_function_type_array (t, n, args);
a0274e3e
JJ
5241
5242 egress:
5243 builtin_types[def] = t;
0edf1bb2 5244 va_end (list);
a0274e3e
JJ
5245}
5246
c6d86fce
ILT
5247/* Build builtin functions common to both C and C++ language
5248 frontends. */
5249
5250static void
5251c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5252{
5253#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5254 builtin_types[ENUM] = VALUE;
5255#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5256 def_fn_type (ENUM, RETURN, 0, 0);
5257#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5258 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5259#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5260 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5261#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5262 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5263#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5264 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5265#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5266 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5267#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5268 ARG6) \
5269 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5270#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5271 ARG6, ARG7) \
5272 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
acf0174b
JJ
5273#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5274 ARG6, ARG7, ARG8) \
5275 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5276 ARG7, ARG8);
d9a6bd32
JJ
5277#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5278 ARG6, ARG7, ARG8, ARG9) \
5279 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5280 ARG7, ARG8, ARG9);
5281#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5282 ARG6, ARG7, ARG8, ARG9, ARG10) \
5283 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5284 ARG7, ARG8, ARG9, ARG10);
5285#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5286 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5287 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5288 ARG7, ARG8, ARG9, ARG10, ARG11);
c6d86fce
ILT
5289#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5290 def_fn_type (ENUM, RETURN, 1, 0);
5291#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5292 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5293#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5294 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5295#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5296 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5297#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5298 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5299#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5300 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3e32ee19
NS
5301#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5302 ARG6) \
5303 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
56a9f6bc
TS
5304#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5305 ARG6, ARG7) \
5306 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
c6d86fce
ILT
5307#define DEF_POINTER_TYPE(ENUM, TYPE) \
5308 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5309
5310#include "builtin-types.def"
5311
5312#undef DEF_PRIMITIVE_TYPE
f6a7cffc 5313#undef DEF_FUNCTION_TYPE_0
c6d86fce
ILT
5314#undef DEF_FUNCTION_TYPE_1
5315#undef DEF_FUNCTION_TYPE_2
5316#undef DEF_FUNCTION_TYPE_3
5317#undef DEF_FUNCTION_TYPE_4
5318#undef DEF_FUNCTION_TYPE_5
5319#undef DEF_FUNCTION_TYPE_6
f6a7cffc
TS
5320#undef DEF_FUNCTION_TYPE_7
5321#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5322#undef DEF_FUNCTION_TYPE_9
5323#undef DEF_FUNCTION_TYPE_10
5324#undef DEF_FUNCTION_TYPE_11
c6d86fce
ILT
5325#undef DEF_FUNCTION_TYPE_VAR_0
5326#undef DEF_FUNCTION_TYPE_VAR_1
5327#undef DEF_FUNCTION_TYPE_VAR_2
5328#undef DEF_FUNCTION_TYPE_VAR_3
5329#undef DEF_FUNCTION_TYPE_VAR_4
5330#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5331#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5332#undef DEF_FUNCTION_TYPE_VAR_7
c6d86fce
ILT
5333#undef DEF_POINTER_TYPE
5334 builtin_types[(int) BT_LAST] = NULL_TREE;
5335
5336 c_init_attributes ();
5337
5338#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5339 NONANSI_P, ATTRS, IMPLICIT, COND) \
5340 if (NAME && COND) \
5341 def_builtin_1 (ENUM, NAME, CLASS, \
5342 builtin_types[(int) TYPE], \
5343 builtin_types[(int) LIBTYPE], \
5344 BOTH_P, FALLBACK_P, NONANSI_P, \
5345 built_in_attributes[(int) ATTRS], IMPLICIT);
5346#include "builtins.def"
c6d86fce 5347
8de7ef2a
UB
5348 targetm.init_builtins ();
5349
384c400a 5350 build_common_builtin_nodes ();
939b37da 5351
b72271b9 5352 if (flag_cilkplus)
939b37da 5353 cilk_init_builtins ();
c6d86fce
ILT
5354}
5355
c1b61fca
JM
5356/* Like get_identifier, but avoid warnings about null arguments when
5357 the argument may be NULL for targets where GCC lacks stdint.h type
5358 information. */
5359
5360static inline tree
5361c_get_ident (const char *id)
5362{
5363 return get_identifier (id);
5364}
5365
a0274e3e
JJ
5366/* Build tree nodes and builtin functions common to both C and C++ language
5367 frontends. */
5368
5369void
5370c_common_nodes_and_builtins (void)
5371{
b6baa67d
KVH
5372 int char16_type_size;
5373 int char32_type_size;
eaa7c03f
JM
5374 int wchar_type_size;
5375 tree array_domain_type;
9f720c3e 5376 tree va_list_ref_type_node;
daf68dd7 5377 tree va_list_arg_type_node;
78a7c317 5378 int i;
d3707adb 5379
a011cd92 5380 build_common_tree_nodes (flag_signed_char);
fce5dddd 5381
eaa7c03f 5382 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 5383 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
5384 record_builtin_type (RID_CHAR, "char", char_type_node);
5385
5386 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5387 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5388 but not C. Are the conditionals here needed? */
37fa72e9 5389 if (c_dialect_cxx ())
6496a589 5390 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
5391 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5392 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5393 record_builtin_type (RID_MAX, "long unsigned int",
5394 long_unsigned_type_node);
78a7c317
DD
5395
5396 for (i = 0; i < NUM_INT_N_ENTS; i ++)
a6766312 5397 {
78a7c317
DD
5398 char name[25];
5399
5400 sprintf (name, "__int%d", int_n_data[i].bitsize);
17958621 5401 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
78a7c317
DD
5402 int_n_trees[i].signed_type);
5403 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
17958621 5404 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
a6766312 5405 }
78a7c317 5406
37fa72e9 5407 if (c_dialect_cxx ())
eaa7c03f
JM
5408 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5409 record_builtin_type (RID_MAX, "long long int",
5410 long_long_integer_type_node);
5411 record_builtin_type (RID_MAX, "long long unsigned int",
5412 long_long_unsigned_type_node);
37fa72e9 5413 if (c_dialect_cxx ())
eaa7c03f
JM
5414 record_builtin_type (RID_MAX, "long long unsigned",
5415 long_long_unsigned_type_node);
5416 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5417 record_builtin_type (RID_MAX, "short unsigned int",
5418 short_unsigned_type_node);
37fa72e9 5419 if (c_dialect_cxx ())
eaa7c03f
JM
5420 record_builtin_type (RID_MAX, "unsigned short",
5421 short_unsigned_type_node);
5422
5423 /* Define both `signed char' and `unsigned char'. */
5424 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5425 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5426
b0c48229
NB
5427 /* These are types that c_common_type_for_size and
5428 c_common_type_for_mode use. */
c2255bc4
AH
5429 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5430 TYPE_DECL, NULL_TREE,
ae2bcd98 5431 intQI_type_node));
c2255bc4
AH
5432 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5433 TYPE_DECL, NULL_TREE,
ae2bcd98 5434 intHI_type_node));
c2255bc4
AH
5435 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5436 TYPE_DECL, NULL_TREE,
ae2bcd98 5437 intSI_type_node));
c2255bc4
AH
5438 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5439 TYPE_DECL, NULL_TREE,
ae2bcd98 5440 intDI_type_node));
eaa7c03f 5441#if HOST_BITS_PER_WIDE_INT >= 64
78a7c317
DD
5442 /* Note that this is different than the __int128 type that's part of
5443 the generic __intN support. */
1e1b8649 5444 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5445 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5446 TYPE_DECL,
1e1b8649
AP
5447 get_identifier ("__int128_t"),
5448 intTI_type_node));
eaa7c03f 5449#endif
c2255bc4
AH
5450 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5451 TYPE_DECL, NULL_TREE,
ae2bcd98 5452 unsigned_intQI_type_node));
c2255bc4
AH
5453 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5454 TYPE_DECL, NULL_TREE,
ae2bcd98 5455 unsigned_intHI_type_node));
c2255bc4
AH
5456 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5457 TYPE_DECL, NULL_TREE,
ae2bcd98 5458 unsigned_intSI_type_node));
c2255bc4
AH
5459 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5460 TYPE_DECL, NULL_TREE,
ae2bcd98 5461 unsigned_intDI_type_node));
eaa7c03f 5462#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649 5463 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5464 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5465 TYPE_DECL,
1e1b8649
AP
5466 get_identifier ("__uint128_t"),
5467 unsigned_intTI_type_node));
eaa7c03f
JM
5468#endif
5469
5470 /* Create the widest literal types. */
5471 widest_integer_literal_type_node
5472 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5473 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5474 TYPE_DECL, NULL_TREE,
ae2bcd98 5475 widest_integer_literal_type_node));
eaa7c03f
JM
5476
5477 widest_unsigned_literal_type_node
5478 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5479 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5480 TYPE_DECL, NULL_TREE,
ae2bcd98 5481 widest_unsigned_literal_type_node));
eaa7c03f 5482
c9f8536c 5483 signed_size_type_node = c_common_signed_type (size_type_node);
eaa7c03f 5484
d1c38823
ZD
5485 pid_type_node =
5486 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5487
6496a589
KG
5488 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5489 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
5490 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5491
c65699ef
JM
5492 if (!c_dialect_cxx ())
5493 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
5494 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
5495 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
5496 FLOATN_NX_TYPE_NODE (i));
5497
9a8ce21f
JG
5498 /* Only supported decimal floating point extension if the target
5499 actually supports underlying modes. */
b8698a0f 5500 if (targetm.scalar_mode_supported_p (SDmode)
9a8ce21f
JG
5501 && targetm.scalar_mode_supported_p (DDmode)
5502 && targetm.scalar_mode_supported_p (TDmode))
5503 {
5504 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5505 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5506 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5507 }
5508
ab22c1fa
CF
5509 if (targetm.fixed_point_supported_p ())
5510 {
5511 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5512 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5513 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5514 record_builtin_type (RID_MAX, "long long _Fract",
5515 long_long_fract_type_node);
5516 record_builtin_type (RID_MAX, "unsigned short _Fract",
5517 unsigned_short_fract_type_node);
5518 record_builtin_type (RID_MAX, "unsigned _Fract",
5519 unsigned_fract_type_node);
5520 record_builtin_type (RID_MAX, "unsigned long _Fract",
5521 unsigned_long_fract_type_node);
5522 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5523 unsigned_long_long_fract_type_node);
5524 record_builtin_type (RID_MAX, "_Sat short _Fract",
5525 sat_short_fract_type_node);
5526 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5527 record_builtin_type (RID_MAX, "_Sat long _Fract",
5528 sat_long_fract_type_node);
5529 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5530 sat_long_long_fract_type_node);
5531 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5532 sat_unsigned_short_fract_type_node);
5533 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5534 sat_unsigned_fract_type_node);
5535 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5536 sat_unsigned_long_fract_type_node);
5537 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5538 sat_unsigned_long_long_fract_type_node);
5539 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5540 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5541 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5542 record_builtin_type (RID_MAX, "long long _Accum",
5543 long_long_accum_type_node);
5544 record_builtin_type (RID_MAX, "unsigned short _Accum",
5545 unsigned_short_accum_type_node);
5546 record_builtin_type (RID_MAX, "unsigned _Accum",
5547 unsigned_accum_type_node);
5548 record_builtin_type (RID_MAX, "unsigned long _Accum",
5549 unsigned_long_accum_type_node);
5550 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5551 unsigned_long_long_accum_type_node);
5552 record_builtin_type (RID_MAX, "_Sat short _Accum",
5553 sat_short_accum_type_node);
5554 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5555 record_builtin_type (RID_MAX, "_Sat long _Accum",
5556 sat_long_accum_type_node);
5557 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5558 sat_long_long_accum_type_node);
5559 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5560 sat_unsigned_short_accum_type_node);
5561 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5562 sat_unsigned_accum_type_node);
5563 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5564 sat_unsigned_long_accum_type_node);
5565 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5566 sat_unsigned_long_long_accum_type_node);
5567
5568 }
5569
c2255bc4
AH
5570 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5571 TYPE_DECL,
ae2bcd98
RS
5572 get_identifier ("complex int"),
5573 complex_integer_type_node));
c2255bc4
AH
5574 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5575 TYPE_DECL,
ae2bcd98
RS
5576 get_identifier ("complex float"),
5577 complex_float_type_node));
c2255bc4
AH
5578 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5579 TYPE_DECL,
ae2bcd98
RS
5580 get_identifier ("complex double"),
5581 complex_double_type_node));
5582 lang_hooks.decls.pushdecl
c2255bc4
AH
5583 (build_decl (UNKNOWN_LOCATION,
5584 TYPE_DECL, get_identifier ("complex long double"),
43577e6b 5585 complex_long_double_type_node));
eaa7c03f 5586
c65699ef
JM
5587 if (!c_dialect_cxx ())
5588 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
5589 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
5590 {
5591 char buf[30];
5592 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
5593 floatn_nx_types[i].extended ? "x" : "");
5594 lang_hooks.decls.pushdecl
5595 (build_decl (UNKNOWN_LOCATION,
5596 TYPE_DECL,
5597 get_identifier (buf),
5598 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
5599 }
5600
498c0f27
JJ
5601 if (c_dialect_cxx ())
5602 /* For C++, make fileptr_type_node a distinct void * type until
5603 FILE type is defined. */
8dd16ecc 5604 fileptr_type_node = build_variant_type_copy (ptr_type_node);
498c0f27 5605
6496a589 5606 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 5607
06d40de8
DG
5608 /* Set the TYPE_NAME for any variants that were built before
5609 record_builtin_type gave names to the built-in types. */
5610 {
5611 tree void_name = TYPE_NAME (void_type_node);
5612 TYPE_NAME (void_type_node) = NULL_TREE;
5613 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5614 = void_name;
5615 TYPE_NAME (void_type_node) = void_name;
5616 }
5617
eaa7c03f
JM
5618 void_list_node = build_void_list_node ();
5619
5620 /* Make a type to be the domain of a few array types
5621 whose domains don't really matter.
5622 200 is small enough that it always fits in size_t
5623 and large enough that it can hold most function names for the
5624 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5625 array_domain_type = build_index_type (size_int (200));
5626
5627 /* Make a type for arrays of characters.
5628 With luck nothing will ever really depend on the length of this
5629 array type. */
5630 char_array_type_node
5631 = build_array_type (char_type_node, array_domain_type);
5632
10841285
MM
5633 string_type_node = build_pointer_type (char_type_node);
5634 const_string_type_node
5635 = build_pointer_type (build_qualified_type
5636 (char_type_node, TYPE_QUAL_CONST));
5637
eaa7c03f 5638 /* This is special for C++ so functions can be overloaded. */
a11eba95 5639 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
5640 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5641 wchar_type_size = TYPE_PRECISION (wchar_type_node);
207bf79d 5642 underlying_wchar_type_node = wchar_type_node;
37fa72e9 5643 if (c_dialect_cxx ())
eaa7c03f 5644 {
8df83eae 5645 if (TYPE_UNSIGNED (wchar_type_node))
eaa7c03f
JM
5646 wchar_type_node = make_unsigned_type (wchar_type_size);
5647 else
5648 wchar_type_node = make_signed_type (wchar_type_size);
5649 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5650 }
eaa7c03f
JM
5651
5652 /* This is for wide string constants. */
5653 wchar_array_type_node
5654 = build_array_type (wchar_type_node, array_domain_type);
5655
b6baa67d
KVH
5656 /* Define 'char16_t'. */
5657 char16_type_node = get_identifier (CHAR16_TYPE);
5658 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5659 char16_type_size = TYPE_PRECISION (char16_type_node);
5660 if (c_dialect_cxx ())
5661 {
5662 char16_type_node = make_unsigned_type (char16_type_size);
5663
604b2bfc 5664 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5665 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5666 }
5667
5668 /* This is for UTF-16 string constants. */
5669 char16_array_type_node
5670 = build_array_type (char16_type_node, array_domain_type);
5671
5672 /* Define 'char32_t'. */
5673 char32_type_node = get_identifier (CHAR32_TYPE);
5674 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5675 char32_type_size = TYPE_PRECISION (char32_type_node);
5676 if (c_dialect_cxx ())
5677 {
5678 char32_type_node = make_unsigned_type (char32_type_size);
5679
604b2bfc 5680 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5681 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5682 }
5683
5684 /* This is for UTF-32 string constants. */
5685 char32_array_type_node
5686 = build_array_type (char32_type_node, array_domain_type);
5687
5fd8e536
JM
5688 wint_type_node =
5689 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5690
5691 intmax_type_node =
5692 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5693 uintmax_type_node =
5694 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5695
207bf79d
JM
5696 if (SIG_ATOMIC_TYPE)
5697 sig_atomic_type_node =
c1b61fca 5698 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
207bf79d
JM
5699 if (INT8_TYPE)
5700 int8_type_node =
c1b61fca 5701 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
207bf79d
JM
5702 if (INT16_TYPE)
5703 int16_type_node =
c1b61fca 5704 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
207bf79d
JM
5705 if (INT32_TYPE)
5706 int32_type_node =
c1b61fca 5707 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
207bf79d
JM
5708 if (INT64_TYPE)
5709 int64_type_node =
c1b61fca 5710 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
207bf79d
JM
5711 if (UINT8_TYPE)
5712 uint8_type_node =
c1b61fca 5713 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
207bf79d 5714 if (UINT16_TYPE)
cca615af 5715 c_uint16_type_node = uint16_type_node =
c1b61fca 5716 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
207bf79d 5717 if (UINT32_TYPE)
cca615af 5718 c_uint32_type_node = uint32_type_node =
c1b61fca 5719 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
207bf79d 5720 if (UINT64_TYPE)
cca615af 5721 c_uint64_type_node = uint64_type_node =
c1b61fca 5722 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
207bf79d
JM
5723 if (INT_LEAST8_TYPE)
5724 int_least8_type_node =
c1b61fca 5725 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
207bf79d
JM
5726 if (INT_LEAST16_TYPE)
5727 int_least16_type_node =
c1b61fca 5728 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
207bf79d
JM
5729 if (INT_LEAST32_TYPE)
5730 int_least32_type_node =
c1b61fca 5731 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
207bf79d
JM
5732 if (INT_LEAST64_TYPE)
5733 int_least64_type_node =
c1b61fca 5734 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
207bf79d
JM
5735 if (UINT_LEAST8_TYPE)
5736 uint_least8_type_node =
c1b61fca 5737 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
207bf79d
JM
5738 if (UINT_LEAST16_TYPE)
5739 uint_least16_type_node =
c1b61fca 5740 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
207bf79d
JM
5741 if (UINT_LEAST32_TYPE)
5742 uint_least32_type_node =
c1b61fca 5743 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
207bf79d
JM
5744 if (UINT_LEAST64_TYPE)
5745 uint_least64_type_node =
c1b61fca 5746 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
207bf79d
JM
5747 if (INT_FAST8_TYPE)
5748 int_fast8_type_node =
c1b61fca 5749 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
207bf79d
JM
5750 if (INT_FAST16_TYPE)
5751 int_fast16_type_node =
c1b61fca 5752 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
207bf79d
JM
5753 if (INT_FAST32_TYPE)
5754 int_fast32_type_node =
c1b61fca 5755 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
207bf79d
JM
5756 if (INT_FAST64_TYPE)
5757 int_fast64_type_node =
c1b61fca 5758 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
207bf79d
JM
5759 if (UINT_FAST8_TYPE)
5760 uint_fast8_type_node =
c1b61fca 5761 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
207bf79d
JM
5762 if (UINT_FAST16_TYPE)
5763 uint_fast16_type_node =
c1b61fca 5764 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
207bf79d
JM
5765 if (UINT_FAST32_TYPE)
5766 uint_fast32_type_node =
c1b61fca 5767 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
207bf79d
JM
5768 if (UINT_FAST64_TYPE)
5769 uint_fast64_type_node =
c1b61fca 5770 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
207bf79d
JM
5771 if (INTPTR_TYPE)
5772 intptr_type_node =
c1b61fca 5773 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
207bf79d
JM
5774 if (UINTPTR_TYPE)
5775 uintptr_type_node =
c1b61fca 5776 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
207bf79d 5777
8242dd04
NF
5778 default_function_type
5779 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5fd8e536
JM
5780 ptrdiff_type_node
5781 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
12753674 5782 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 5783
ae2bcd98 5784 lang_hooks.decls.pushdecl
c2255bc4
AH
5785 (build_decl (UNKNOWN_LOCATION,
5786 TYPE_DECL, get_identifier ("__builtin_va_list"),
43577e6b 5787 va_list_type_node));
38f8b050 5788 if (targetm.enum_va_list_p)
d4048208
KT
5789 {
5790 int l;
5791 const char *pname;
5792 tree ptype;
35cbb299 5793
38f8b050 5794 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
d4048208
KT
5795 {
5796 lang_hooks.decls.pushdecl
5797 (build_decl (UNKNOWN_LOCATION,
5798 TYPE_DECL, get_identifier (pname),
5799 ptype));
5800
5801 }
5802 }
daf68dd7 5803
daf68dd7 5804 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
5805 {
5806 va_list_arg_type_node = va_list_ref_type_node =
5807 build_pointer_type (TREE_TYPE (va_list_type_node));
5808 }
daf68dd7 5809 else
9f720c3e
GK
5810 {
5811 va_list_arg_type_node = va_list_type_node;
5812 va_list_ref_type_node = build_reference_type (va_list_type_node);
5813 }
35b1a6fa 5814
c6d86fce
ILT
5815 if (!flag_preprocess_only)
5816 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4677862a 5817
5b47282c 5818 main_identifier_node = get_identifier ("main");
b2f97e4a
MM
5819
5820 /* Create the built-in __null node. It is important that this is
5821 not shared. */
807e902e 5822 null_node = make_int_cst (1, 1);
b2f97e4a 5823 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
a0274e3e
JJ
5824
5825 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5826 memset (builtin_types, 0, sizeof (builtin_types));
7f4edbcb 5827}
d3707adb 5828
3b2db49f
MM
5829/* The number of named compound-literals generated thus far. */
5830static GTY(()) int compound_literal_number;
5831
5832/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5833
5834void
5835set_compound_literal_name (tree decl)
5836{
5837 char *name;
5838 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5839 compound_literal_number);
5840 compound_literal_number++;
5841 DECL_NAME (decl) = get_identifier (name);
5842}
5843
2fe1d762
TV
5844/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5845 TYPE and operand OP. */
5846
5847static tree
5848build_va_arg_1 (location_t loc, tree type, tree op)
5849{
5850 tree expr = build1 (VA_ARG_EXPR, type, op);
5851 SET_EXPR_LOCATION (expr, loc);
5852 return expr;
5853}
5854
5855/* Return a VA_ARG_EXPR corresponding to a source-level expression
5856 va_arg (EXPR, TYPE) at source location LOC. */
5857
d3707adb 5858tree
c2255bc4 5859build_va_arg (location_t loc, tree expr, tree type)
d3707adb 5860{
c7b38fd5
TV
5861 tree va_type = TREE_TYPE (expr);
5862 tree canon_va_type = (va_type == error_mark_node
ba9bbd6f 5863 ? error_mark_node
c7b38fd5
TV
5864 : targetm.canonical_va_list_type (va_type));
5865
2fe1d762
TV
5866 if (va_type == error_mark_node
5867 || canon_va_type == NULL_TREE)
c7b38fd5 5868 {
ba9bbd6f
TV
5869 if (canon_va_type == NULL_TREE)
5870 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
5871
2fe1d762
TV
5872 /* Let's handle things neutrallly, if expr:
5873 - has undeclared type, or
5874 - is not an va_list type. */
ba9bbd6f 5875 return build_va_arg_1 (loc, type, error_mark_node);
c7b38fd5 5876 }
ecd0e562 5877
2fe1d762
TV
5878 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5879 {
5880 /* Case 1: Not an array type. */
5881
5b28efbb
TV
5882 /* Take the address, to get '&ap'. Note that &ap is not a va_list
5883 type. */
2fe1d762
TV
5884 mark_addressable (expr);
5885 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5886
2fe1d762
TV
5887 return build_va_arg_1 (loc, type, expr);
5888 }
5889
5890 /* Case 2: Array type.
5891
5892 Background:
5893
5894 For contrast, let's start with the simple case (case 1). If
5895 canon_va_type is not an array type, but say a char *, then when
5896 passing-by-value a va_list, the type of the va_list param decl is
5897 the same as for another va_list decl (all ap's are char *):
5898
5899 f2_1 (char * ap)
5900 D.1815 = VA_ARG (&ap, 0B, 1);
5901 return D.1815;
5902
5903 f2 (int i)
5904 char * ap.0;
5905 char * ap;
5906 __builtin_va_start (&ap, 0);
5907 ap.0 = ap;
5908 res = f2_1 (ap.0);
5909 __builtin_va_end (&ap);
5910 D.1812 = res;
5911 return D.1812;
5912
5913 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5914 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5915 the same as for another va_list decl (case 2a, struct ap[1]).
5916
5917 f2_1 (struct * ap)
5918 D.1844 = VA_ARG (ap, 0B, 0);
5919 return D.1844;
5920
5921 f2 (int i)
5922 struct ap[1];
5923 __builtin_va_start (&ap, 0);
5924 res = f2_1 (&ap);
5925 __builtin_va_end (&ap);
5926 D.1841 = res;
5927 return D.1841;
5928
5929 Case 2b is different because:
5930 - on the callee side, the parm decl has declared type va_list, but
5931 grokdeclarator changes the type of the parm decl to a pointer to the
5932 array elem type.
5933 - on the caller side, the pass-by-value uses &ap.
5934
5935 We unify these two cases (case 2a: va_list is array type,
5936 case 2b: va_list is pointer to array elem type), by adding '&' for the
5937 array type case, such that we have a pointer to array elem in both
5938 cases. */
5939
5940 if (TREE_CODE (va_type) == ARRAY_TYPE)
5941 {
5942 /* Case 2a: va_list is array type. */
5943
5944 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5945 elem type. */
5946 mark_addressable (expr);
5947 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5948 expr);
5949
5950 /* Verify that &ap is still recognized as having va_list type. */
5951 tree canon_expr_type
5952 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5b28efbb 5953 gcc_assert (canon_expr_type != NULL_TREE);
2fe1d762
TV
5954 }
5955 else
5956 {
5957 /* Case 2b: va_list is pointer to array elem type. */
5958 gcc_assert (POINTER_TYPE_P (va_type));
34cedad5
TV
5959
5960 /* Comparison as in std_canonical_va_list_type. */
5961 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
5962 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
2fe1d762
TV
5963
5964 /* Don't take the address. We've already got '&ap'. */
5965 ;
5966 }
5967
5968 return build_va_arg_1 (loc, type, expr);
d3707adb 5969}
fc2aaf30
JM
5970
5971
7d14c755
JM
5972/* Linked list of disabled built-in functions. */
5973
a79683d5 5974struct disabled_builtin
7d14c755
JM
5975{
5976 const char *name;
5977 struct disabled_builtin *next;
a79683d5 5978};
7d14c755
JM
5979static disabled_builtin *disabled_builtins = NULL;
5980
35b1a6fa 5981static bool builtin_function_disabled_p (const char *);
7d14c755
JM
5982
5983/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5984 begins with "__builtin_", give an error. */
5985
5986void
35b1a6fa 5987disable_builtin_function (const char *name)
7d14c755
JM
5988{
5989 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
bda67431 5990 error ("cannot disable built-in function %qs", name);
7d14c755
JM
5991 else
5992 {
1ad463f4
BI
5993 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5994 new_disabled_builtin->name = name;
5995 new_disabled_builtin->next = disabled_builtins;
5996 disabled_builtins = new_disabled_builtin;
7d14c755
JM
5997 }
5998}
5999
6000
6001/* Return true if the built-in function NAME has been disabled, false
6002 otherwise. */
6003
6004static bool
35b1a6fa 6005builtin_function_disabled_p (const char *name)
7d14c755
JM
6006{
6007 disabled_builtin *p;
6008 for (p = disabled_builtins; p != NULL; p = p->next)
6009 {
6010 if (strcmp (name, p->name) == 0)
6011 return true;
6012 }
6013 return false;
6014}
6015
6016
9bc15050
RG
6017/* Worker for DEF_BUILTIN.
6018 Possibly define a builtin function with one or two names.
6019 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6020 nonansi_p and flag_no_nonansi_builtin. */
fc2aaf30 6021
9bc15050
RG
6022static void
6023def_builtin_1 (enum built_in_function fncode,
6024 const char *name,
6025 enum built_in_class fnclass,
6026 tree fntype, tree libtype,
6027 bool both_p, bool fallback_p, bool nonansi_p,
6028 tree fnattrs, bool implicit_p)
fc2aaf30 6029{
9bc15050
RG
6030 tree decl;
6031 const char *libname;
6032
a0274e3e
JJ
6033 if (fntype == error_mark_node)
6034 return;
6035
9bc15050
RG
6036 gcc_assert ((!both_p && !fallback_p)
6037 || !strncmp (name, "__builtin_",
6038 strlen ("__builtin_")));
6039
6040 libname = name + strlen ("__builtin_");
c79efc4d
RÁE
6041 decl = add_builtin_function (name, fntype, fncode, fnclass,
6042 (fallback_p ? libname : NULL),
6043 fnattrs);
e79983f4
MM
6044
6045 set_builtin_decl (fncode, decl, implicit_p);
6046
9bc15050
RG
6047 if (both_p
6048 && !flag_no_builtin && !builtin_function_disabled_p (libname)
7d14c755 6049 && !(nonansi_p && flag_no_nonansi_builtin))
c79efc4d
RÁE
6050 add_builtin_function (libname, libtype, fncode, fnclass,
6051 NULL, fnattrs);
fc2aaf30 6052}
c530479e 6053\f
d72040f5
RH
6054/* Nonzero if the type T promotes to int. This is (nearly) the
6055 integral promotions defined in ISO C99 6.3.1.1/2. */
6056
6057bool
58f9752a 6058c_promoting_integer_type_p (const_tree t)
d72040f5
RH
6059{
6060 switch (TREE_CODE (t))
6061 {
6062 case INTEGER_TYPE:
6063 return (TYPE_MAIN_VARIANT (t) == char_type_node
6064 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6065 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6066 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
6067 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6068 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
6069
6070 case ENUMERAL_TYPE:
6071 /* ??? Technically all enumerations not larger than an int
6072 promote to an int. But this is used along code paths
6073 that only want to notice a size change. */
6074 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6075
6076 case BOOLEAN_TYPE:
6077 return 1;
6078
6079 default:
6080 return 0;
6081 }
6082}
6083
c530479e
RH
6084/* Return 1 if PARMS specifies a fixed number of parameters
6085 and none of their types is affected by default promotions. */
6086
6087int
58f9752a 6088self_promoting_args_p (const_tree parms)
c530479e 6089{
58f9752a 6090 const_tree t;
c530479e
RH
6091 for (t = parms; t; t = TREE_CHAIN (t))
6092 {
b3694847 6093 tree type = TREE_VALUE (t);
7e8176d7 6094
694fea20
VR
6095 if (type == error_mark_node)
6096 continue;
6097
c530479e
RH
6098 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6099 return 0;
6100
6101 if (type == 0)
6102 return 0;
6103
6104 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6105 return 0;
6106
d72040f5 6107 if (c_promoting_integer_type_p (type))
c530479e
RH
6108 return 0;
6109 }
6110 return 1;
6111}
5eda3d66 6112
12ea3302
GDR
6113/* Recursively remove any '*' or '&' operator from TYPE. */
6114tree
6115strip_pointer_operator (tree t)
6116{
6117 while (POINTER_TYPE_P (t))
6118 t = TREE_TYPE (t);
6119 return t;
6120}
6121
ba992967
SP
6122/* Recursively remove pointer or array type from TYPE. */
6123tree
6124strip_pointer_or_array_types (tree t)
6125{
6126 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6127 t = TREE_TYPE (t);
6128 return t;
6129}
6130
8f17b5c5
MM
6131/* Used to compare case labels. K1 and K2 are actually tree nodes
6132 representing case labels, or NULL_TREE for a `default' label.
6133 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6134 K2, and 0 if K1 and K2 are equal. */
6135
6136int
35b1a6fa 6137case_compare (splay_tree_key k1, splay_tree_key k2)
8f17b5c5
MM
6138{
6139 /* Consider a NULL key (such as arises with a `default' label) to be
6140 smaller than anything else. */
6141 if (!k1)
6142 return k2 ? -1 : 0;
6143 else if (!k2)
6144 return k1 ? 1 : 0;
6145
6146 return tree_int_cst_compare ((tree) k1, (tree) k2);
6147}
6148
c2255bc4
AH
6149/* Process a case label, located at LOC, for the range LOW_VALUE
6150 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6151 then this case label is actually a `default' label. If only
6152 HIGH_VALUE is NULL_TREE, then case label was declared using the
6153 usual C/C++ syntax, rather than the GNU case range extension.
6154 CASES is a tree containing all the case ranges processed so far;
b155cfd9
MP
6155 COND is the condition for the switch-statement itself.
6156 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6157 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6158 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
8f17b5c5
MM
6159
6160tree
c2255bc4 6161c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
b155cfd9 6162 tree low_value, tree high_value, bool *outside_range_p)
8f17b5c5
MM
6163{
6164 tree type;
6165 tree label;
6166 tree case_label;
6167 splay_tree_node node;
6168
6169 /* Create the LABEL_DECL itself. */
c2255bc4 6170 label = create_artificial_label (loc);
8f17b5c5
MM
6171
6172 /* If there was an error processing the switch condition, bail now
6173 before we get more confused. */
6174 if (!cond || cond == error_mark_node)
6de9cd9a 6175 goto error_out;
8f17b5c5 6176
35b1a6fa
AJ
6177 if ((low_value && TREE_TYPE (low_value)
6178 && POINTER_TYPE_P (TREE_TYPE (low_value)))
8f17b5c5
MM
6179 || (high_value && TREE_TYPE (high_value)
6180 && POINTER_TYPE_P (TREE_TYPE (high_value))))
522ddfa2 6181 {
c2255bc4 6182 error_at (loc, "pointers are not permitted as case values");
522ddfa2
JM
6183 goto error_out;
6184 }
8f17b5c5
MM
6185
6186 /* Case ranges are a GNU extension. */
fcf73884 6187 if (high_value)
c1771a20 6188 pedwarn (loc, OPT_Wpedantic,
fcf73884 6189 "range expressions in switch statements are non-standard");
8f17b5c5
MM
6190
6191 type = TREE_TYPE (cond);
6192 if (low_value)
6193 {
62e4eb35 6194 low_value = check_case_value (loc, low_value);
68fca595 6195 low_value = convert_and_check (loc, type, low_value);
c0e22534
NS
6196 if (low_value == error_mark_node)
6197 goto error_out;
8f17b5c5
MM
6198 }
6199 if (high_value)
6200 {
62e4eb35 6201 high_value = check_case_value (loc, high_value);
68fca595 6202 high_value = convert_and_check (loc, type, high_value);
c0e22534
NS
6203 if (high_value == error_mark_node)
6204 goto error_out;
8f17b5c5
MM
6205 }
6206
c0e22534
NS
6207 if (low_value && high_value)
6208 {
6209 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
c22cacf3
MS
6210 really a case range, even though it was written that way.
6211 Remove the HIGH_VALUE to simplify later processing. */
c0e22534
NS
6212 if (tree_int_cst_equal (low_value, high_value))
6213 high_value = NULL_TREE;
6214 else if (!tree_int_cst_lt (low_value, high_value))
c2255bc4 6215 warning_at (loc, 0, "empty range specified");
c0e22534 6216 }
8f17b5c5 6217
a6c0a76c
SB
6218 /* See if the case is in range of the type of the original testing
6219 expression. If both low_value and high_value are out of range,
6220 don't insert the case label and return NULL_TREE. */
6221 if (low_value
9d548dfb 6222 && !check_case_bounds (loc, type, orig_type,
b155cfd9
MP
6223 &low_value, high_value ? &high_value : NULL,
6224 outside_range_p))
a6c0a76c
SB
6225 return NULL_TREE;
6226
8f17b5c5
MM
6227 /* Look up the LOW_VALUE in the table of case labels we already
6228 have. */
6229 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6230 /* If there was not an exact match, check for overlapping ranges.
6231 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6232 that's a `default' label and the only overlap is an exact match. */
6233 if (!node && (low_value || high_value))
6234 {
6235 splay_tree_node low_bound;
6236 splay_tree_node high_bound;
6237
6238 /* Even though there wasn't an exact match, there might be an
6239 overlap between this case range and another case range.
6240 Since we've (inductively) not allowed any overlapping case
6241 ranges, we simply need to find the greatest low case label
6242 that is smaller that LOW_VALUE, and the smallest low case
6243 label that is greater than LOW_VALUE. If there is an overlap
6244 it will occur in one of these two ranges. */
6245 low_bound = splay_tree_predecessor (cases,
6246 (splay_tree_key) low_value);
6247 high_bound = splay_tree_successor (cases,
6248 (splay_tree_key) low_value);
6249
6250 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6251 the LOW_VALUE, so there is no need to check unless the
6252 LOW_BOUND is in fact itself a case range. */
6253 if (low_bound
6254 && CASE_HIGH ((tree) low_bound->value)
6255 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6256 low_value) >= 0)
6257 node = low_bound;
6258 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6259 range is bigger than the low end of the current range, so we
6260 are only interested if the current range is a real range, and
6261 not an ordinary case label. */
35b1a6fa 6262 else if (high_bound
8f17b5c5
MM
6263 && high_value
6264 && (tree_int_cst_compare ((tree) high_bound->key,
6265 high_value)
6266 <= 0))
6267 node = high_bound;
6268 }
6269 /* If there was an overlap, issue an error. */
6270 if (node)
6271 {
8c161995 6272 tree duplicate = CASE_LABEL ((tree) node->value);
8f17b5c5
MM
6273
6274 if (high_value)
6275 {
c2255bc4
AH
6276 error_at (loc, "duplicate (or overlapping) case value");
6277 error_at (DECL_SOURCE_LOCATION (duplicate),
6278 "this is the first entry overlapping that value");
8f17b5c5
MM
6279 }
6280 else if (low_value)
6281 {
c2255bc4
AH
6282 error_at (loc, "duplicate case value") ;
6283 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
8f17b5c5
MM
6284 }
6285 else
6286 {
c2255bc4
AH
6287 error_at (loc, "multiple default labels in one switch");
6288 error_at (DECL_SOURCE_LOCATION (duplicate),
6289 "this is the first default label");
8f17b5c5 6290 }
6de9cd9a 6291 goto error_out;
8f17b5c5
MM
6292 }
6293
6294 /* Add a CASE_LABEL to the statement-tree. */
3d528853 6295 case_label = add_stmt (build_case_label (low_value, high_value, label));
8f17b5c5 6296 /* Register this case label in the splay tree. */
35b1a6fa 6297 splay_tree_insert (cases,
8f17b5c5
MM
6298 (splay_tree_key) low_value,
6299 (splay_tree_value) case_label);
6300
6301 return case_label;
6de9cd9a
DN
6302
6303 error_out:
9e14e18f 6304 /* Add a label so that the back-end doesn't think that the beginning of
6de9cd9a 6305 the switch is unreachable. Note that we do not add a case label, as
41806d92 6306 that just leads to duplicates and thence to failure later on. */
6de9cd9a
DN
6307 if (!cases->root)
6308 {
c2255bc4
AH
6309 tree t = create_artificial_label (loc);
6310 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6de9cd9a
DN
6311 }
6312 return error_mark_node;
6313}
6314
6315/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6316 Used to verify that case values match up with enumerator values. */
6317
6318static void
6319match_case_to_enum_1 (tree key, tree type, tree label)
6320{
807e902e
KZ
6321 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6322
6323 if (tree_fits_uhwi_p (key))
6324 print_dec (key, buf, UNSIGNED);
6325 else if (tree_fits_shwi_p (key))
6326 print_dec (key, buf, SIGNED);
6de9cd9a 6327 else
807e902e 6328 print_hex (key, buf);
6de9cd9a
DN
6329
6330 if (TYPE_NAME (type) == 0)
c5d75364
MLI
6331 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6332 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6333 "case value %qs not in enumerated type",
6334 buf);
6de9cd9a 6335 else
c5d75364
MLI
6336 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6337 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6338 "case value %qs not in enumerated type %qT",
6339 buf, type);
6de9cd9a
DN
6340}
6341
c782c2fe
RS
6342/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6343 Used to verify that case values match up with enumerator values. */
6344
6de9cd9a
DN
6345static int
6346match_case_to_enum (splay_tree_node node, void *data)
6347{
6348 tree label = (tree) node->value;
28dab132 6349 tree type = (tree) data;
6de9cd9a
DN
6350
6351 /* Skip default case. */
6352 if (!CASE_LOW (label))
6353 return 0;
6354
c782c2fe 6355 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6de9cd9a 6356 when we did our enum->case scan. Reset our scratch bit after. */
c782c2fe 6357 if (!CASE_LOW_SEEN (label))
6de9cd9a
DN
6358 match_case_to_enum_1 (CASE_LOW (label), type, label);
6359 else
c782c2fe 6360 CASE_LOW_SEEN (label) = 0;
6de9cd9a 6361
c782c2fe
RS
6362 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6363 not set, that means that CASE_HIGH did not appear when we did our
6364 enum->case scan. Reset our scratch bit after. */
6de9cd9a
DN
6365 if (CASE_HIGH (label))
6366 {
c782c2fe
RS
6367 if (!CASE_HIGH_SEEN (label))
6368 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6369 else
6370 CASE_HIGH_SEEN (label) = 0;
6de9cd9a
DN
6371 }
6372
6373 return 0;
6374}
6375
fbc315db
ILT
6376/* Handle -Wswitch*. Called from the front end after parsing the
6377 switch construct. */
6378/* ??? Should probably be somewhere generic, since other languages
6379 besides C and C++ would want this. At the moment, however, C/C++
6380 are the only tree-ssa languages that support enumerations at all,
6381 so the point is moot. */
6de9cd9a 6382
fbc315db
ILT
6383void
6384c_do_switch_warnings (splay_tree cases, location_t switch_location,
b155cfd9
MP
6385 tree type, tree cond, bool bool_cond_p,
6386 bool outside_range_p)
6de9cd9a 6387{
9f63daea 6388 splay_tree_node default_node;
c782c2fe
RS
6389 splay_tree_node node;
6390 tree chain;
6de9cd9a 6391
b155cfd9
MP
6392 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6393 && !warn_switch_bool)
6de9cd9a
DN
6394 return;
6395
6de9cd9a 6396 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
44c21c7f 6397 if (!default_node)
fab922b1
MLI
6398 warning_at (switch_location, OPT_Wswitch_default,
6399 "switch missing default case");
6de9cd9a 6400
b155cfd9
MP
6401 /* There are certain cases where -Wswitch-bool warnings aren't
6402 desirable, such as
6403 switch (boolean)
6404 {
6405 case true: ...
6406 case false: ...
6407 }
6408 so be careful here. */
6409 if (warn_switch_bool && bool_cond_p)
6410 {
6411 splay_tree_node min_node;
6412 /* If there's a default node, it's also the value with the minimal
6413 key. So look at the penultimate key (if any). */
6414 if (default_node)
6415 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6416 else
6417 min_node = splay_tree_min (cases);
6418 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6419
6420 splay_tree_node max_node = splay_tree_max (cases);
6421 /* This might be a case range, so look at the value with the
6422 maximal key and then check CASE_HIGH. */
6423 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6424 if (max)
6425 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6426
6427 /* If there's a case value > 1 or < 0, that is outside bool
6428 range, warn. */
6429 if (outside_range_p
6430 || (max && wi::gts_p (max, 1))
6431 || (min && wi::lts_p (min, 0))
6432 /* And handle the
6433 switch (boolean)
6434 {
6435 case true: ...
6436 case false: ...
6437 default: ...
6438 }
6439 case, where we want to warn. */
6440 || (default_node
6441 && max && wi::eq_p (max, 1)
6442 && min && wi::eq_p (min, 0)))
6443 warning_at (switch_location, OPT_Wswitch_bool,
6444 "switch condition has boolean value");
6445 }
6446
026c3cfd 6447 /* From here on, we only care about enumerated types. */
c782c2fe
RS
6448 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6449 return;
6450
cdb88468
ILT
6451 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6452 if (!warn_switch_enum && !warn_switch)
c782c2fe
RS
6453 return;
6454
cdb88468
ILT
6455 /* Check the cases. Warn about case values which are not members of
6456 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6457 there is no default case, check that exactly all enumeration
6458 literals are covered by the cases. */
6459
c782c2fe
RS
6460 /* Clearing COND if it is not an integer constant simplifies
6461 the tests inside the loop below. */
6462 if (TREE_CODE (cond) != INTEGER_CST)
6463 cond = NULL_TREE;
6464
6465 /* The time complexity here is O(N*lg(N)) worst case, but for the
6466 common case of monotonically increasing enumerators, it is
6467 O(N), since the nature of the splay tree will keep the next
6468 element adjacent to the root at all times. */
6de9cd9a 6469
c782c2fe
RS
6470 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6471 {
6472 tree value = TREE_VALUE (chain);
adf2edec
DG
6473 if (TREE_CODE (value) == CONST_DECL)
6474 value = DECL_INITIAL (value);
c782c2fe
RS
6475 node = splay_tree_lookup (cases, (splay_tree_key) value);
6476 if (node)
6de9cd9a 6477 {
c782c2fe
RS
6478 /* Mark the CASE_LOW part of the case entry as seen. */
6479 tree label = (tree) node->value;
6480 CASE_LOW_SEEN (label) = 1;
6481 continue;
6482 }
6483
6484 /* Even though there wasn't an exact match, there might be a
fa10beec 6485 case range which includes the enumerator's value. */
c782c2fe
RS
6486 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6487 if (node && CASE_HIGH ((tree) node->value))
6488 {
6489 tree label = (tree) node->value;
6490 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6491 if (cmp >= 0)
6de9cd9a 6492 {
c782c2fe
RS
6493 /* If we match the upper bound exactly, mark the CASE_HIGH
6494 part of the case entry as seen. */
6495 if (cmp == 0)
6496 CASE_HIGH_SEEN (label) = 1;
6497 continue;
6de9cd9a
DN
6498 }
6499 }
6500
c782c2fe
RS
6501 /* We've now determined that this enumerated literal isn't
6502 handled by the case labels of the switch statement. */
6de9cd9a 6503
c782c2fe
RS
6504 /* If the switch expression is a constant, we only really care
6505 about whether that constant is handled by the switch. */
6506 if (cond && tree_int_cst_compare (cond, value))
6507 continue;
6de9cd9a 6508
683d6ff9 6509 /* If there is a default_node, the only relevant option is
cdb88468 6510 Wswitch-enum. Otherwise, if both are enabled then we prefer
683d6ff9
MLI
6511 to warn using -Wswitch because -Wswitch is enabled by -Wall
6512 while -Wswitch-enum is explicit. */
cdb88468
ILT
6513 warning_at (switch_location,
6514 (default_node || !warn_switch
6515 ? OPT_Wswitch_enum
6516 : OPT_Wswitch),
6517 "enumeration value %qE not handled in switch",
6518 TREE_PURPOSE (chain));
6de9cd9a 6519 }
c782c2fe
RS
6520
6521 /* Warn if there are case expressions that don't correspond to
6522 enumerators. This can occur since C and C++ don't enforce
6523 type-checking of assignments to enumeration variables.
6524
6525 The time complexity here is now always O(N) worst case, since
6526 we should have marked both the lower bound and upper bound of
6527 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6528 above. This scan also resets those fields. */
683d6ff9 6529
c782c2fe 6530 splay_tree_foreach (cases, match_case_to_enum, type);
8f17b5c5
MM
6531}
6532
6b665219 6533/* Finish an expression taking the address of LABEL (an
6a3799eb
AH
6534 IDENTIFIER_NODE). Returns an expression for the address.
6535
6536 LOC is the location for the expression returned. */
15b732b2 6537
35b1a6fa 6538tree
6a3799eb 6539finish_label_address_expr (tree label, location_t loc)
15b732b2
NB
6540{
6541 tree result;
6542
c1771a20 6543 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
15b732b2 6544
6b665219
MM
6545 if (label == error_mark_node)
6546 return error_mark_node;
6547
15b732b2
NB
6548 label = lookup_label (label);
6549 if (label == NULL_TREE)
6550 result = null_pointer_node;
6551 else
6552 {
6553 TREE_USED (label) = 1;
6554 result = build1 (ADDR_EXPR, ptr_type_node, label);
5805e48d 6555 /* The current function is not necessarily uninlinable.
15b732b2
NB
6556 Computed gotos are incompatible with inlining, but the value
6557 here could be used only in a diagnostic, for example. */
6a3799eb 6558 protected_set_expr_location (result, loc);
15b732b2
NB
6559 }
6560
6561 return result;
6562}
19552aa5
JM
6563\f
6564
6565/* Given a boolean expression ARG, return a tree representing an increment
6566 or decrement (as indicated by CODE) of ARG. The front end must check for
6567 invalid cases (e.g., decrement in C++). */
6568tree
35b1a6fa 6569boolean_increment (enum tree_code code, tree arg)
19552aa5
JM
6570{
6571 tree val;
b5119fa1 6572 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
37fa72e9 6573
19552aa5
JM
6574 arg = stabilize_reference (arg);
6575 switch (code)
6576 {
6577 case PREINCREMENT_EXPR:
53fb4de3 6578 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5
JM
6579 break;
6580 case POSTINCREMENT_EXPR:
53fb4de3 6581 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5 6582 arg = save_expr (arg);
53fb4de3
RS
6583 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6584 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6585 break;
6586 case PREDECREMENT_EXPR:
53fb4de3 6587 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6588 invert_truthvalue_loc (input_location, arg));
19552aa5
JM
6589 break;
6590 case POSTDECREMENT_EXPR:
53fb4de3 6591 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6592 invert_truthvalue_loc (input_location, arg));
19552aa5 6593 arg = save_expr (arg);
53fb4de3
RS
6594 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6595 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6596 break;
6597 default:
366de0ce 6598 gcc_unreachable ();
19552aa5
JM
6599 }
6600 TREE_SIDE_EFFECTS (val) = 1;
6601 return val;
6602}
03dc0325 6603\f
207bf79d
JM
6604/* Built-in macros for stddef.h and stdint.h, that require macros
6605 defined in this file. */
460bd0e3 6606void
35b1a6fa 6607c_stddef_cpp_builtins(void)
3df89291 6608{
5279d739
ZW
6609 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6610 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6611 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6612 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
85291069
JM
6613 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6614 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
c466b2cd
KVH
6615 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6616 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
207bf79d
JM
6617 if (SIG_ATOMIC_TYPE)
6618 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6619 if (INT8_TYPE)
6620 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6621 if (INT16_TYPE)
6622 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6623 if (INT32_TYPE)
6624 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6625 if (INT64_TYPE)
6626 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6627 if (UINT8_TYPE)
6628 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6629 if (UINT16_TYPE)
6630 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6631 if (UINT32_TYPE)
6632 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6633 if (UINT64_TYPE)
6634 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6635 if (INT_LEAST8_TYPE)
6636 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6637 if (INT_LEAST16_TYPE)
6638 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6639 if (INT_LEAST32_TYPE)
6640 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6641 if (INT_LEAST64_TYPE)
6642 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6643 if (UINT_LEAST8_TYPE)
6644 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6645 if (UINT_LEAST16_TYPE)
6646 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6647 if (UINT_LEAST32_TYPE)
6648 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6649 if (UINT_LEAST64_TYPE)
6650 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6651 if (INT_FAST8_TYPE)
6652 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6653 if (INT_FAST16_TYPE)
6654 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6655 if (INT_FAST32_TYPE)
6656 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6657 if (INT_FAST64_TYPE)
6658 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6659 if (UINT_FAST8_TYPE)
6660 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6661 if (UINT_FAST16_TYPE)
6662 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6663 if (UINT_FAST32_TYPE)
6664 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6665 if (UINT_FAST64_TYPE)
6666 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6667 if (INTPTR_TYPE)
6668 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6669 if (UINTPTR_TYPE)
6670 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
676997cf
RH
6671}
6672
6431177a 6673static void
35b1a6fa 6674c_init_attributes (void)
6431177a
JM
6675{
6676 /* Fill in the built_in_attributes array. */
4a90aeeb 6677#define DEF_ATTR_NULL_TREE(ENUM) \
6431177a 6678 built_in_attributes[(int) ENUM] = NULL_TREE;
4a90aeeb 6679#define DEF_ATTR_INT(ENUM, VALUE) \
c62c040f 6680 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
e384e6b5
BS
6681#define DEF_ATTR_STRING(ENUM, VALUE) \
6682 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6431177a
JM
6683#define DEF_ATTR_IDENT(ENUM, STRING) \
6684 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6685#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6686 built_in_attributes[(int) ENUM] \
6687 = tree_cons (built_in_attributes[(int) PURPOSE], \
6688 built_in_attributes[(int) VALUE], \
6689 built_in_attributes[(int) CHAIN]);
6431177a
JM
6690#include "builtin-attrs.def"
6691#undef DEF_ATTR_NULL_TREE
6692#undef DEF_ATTR_INT
6693#undef DEF_ATTR_IDENT
6694#undef DEF_ATTR_TREE_LIST
03dc0325 6695}
26f943fd 6696
943f82e7
JM
6697/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6698 identifier as an argument, so the front end shouldn't look it up. */
6699
6700bool
564a129d 6701attribute_takes_identifier_p (const_tree attr_id)
943f82e7 6702{
f231b5ff 6703 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
1b9b91a6
JM
6704 if (spec == NULL)
6705 /* Unknown attribute that we'll end up ignoring, return true so we
6706 don't complain about an identifier argument. */
6707 return true;
6708 else if (!strcmp ("mode", spec->name)
6709 || !strcmp ("format", spec->name)
6710 || !strcmp ("cleanup", spec->name))
564a129d
JM
6711 return true;
6712 else
6713 return targetm.attribute_takes_identifier_p (attr_id);
943f82e7
JM
6714}
6715
349ae713
NB
6716/* Attribute handlers common to C front ends. */
6717
6718/* Handle a "packed" attribute; arguments as in
6719 struct attribute_spec.handler. */
6720
6721static tree
e18476eb 6722handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
a742c759 6723 int flags, bool *no_add_attrs)
349ae713 6724{
c6e4cc53 6725 if (TYPE_P (*node))
349ae713
NB
6726 {
6727 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 6728 *node = build_variant_type_copy (*node);
c6e4cc53 6729 TYPE_PACKED (*node) = 1;
349ae713
NB
6730 }
6731 else if (TREE_CODE (*node) == FIELD_DECL)
646c0835 6732 {
2cd36c22
AN
6733 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6734 /* Still pack bitfields. */
6735 && ! DECL_INITIAL (*node))
646c0835
NS
6736 warning (OPT_Wattributes,
6737 "%qE attribute ignored for field of type %qT",
6738 name, TREE_TYPE (*node));
6739 else
6740 DECL_PACKED (*node) = 1;
6741 }
349ae713 6742 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
c6e4cc53
NS
6743 used for DECL_REGISTER. It wouldn't mean anything anyway.
6744 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6745 that changes what the typedef is typing. */
349ae713
NB
6746 else
6747 {
5c498b10 6748 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6749 *no_add_attrs = true;
6750 }
6751
6752 return NULL_TREE;
6753}
6754
6755/* Handle a "nocommon" attribute; arguments as in
6756 struct attribute_spec.handler. */
6757
6758static tree
35b1a6fa 6759handle_nocommon_attribute (tree *node, tree name,
e18476eb
BI
6760 tree ARG_UNUSED (args),
6761 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6762{
0ae9bd27 6763 if (VAR_P (*node))
349ae713
NB
6764 DECL_COMMON (*node) = 0;
6765 else
6766 {
5c498b10 6767 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6768 *no_add_attrs = true;
6769 }
6770
6771 return NULL_TREE;
6772}
6773
6774/* Handle a "common" attribute; arguments as in
6775 struct attribute_spec.handler. */
6776
6777static tree
e18476eb
BI
6778handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6779 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6780{
0ae9bd27 6781 if (VAR_P (*node))
349ae713
NB
6782 DECL_COMMON (*node) = 1;
6783 else
6784 {
5c498b10 6785 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6786 *no_add_attrs = true;
6787 }
6788
6789 return NULL_TREE;
6790}
6791
6792/* Handle a "noreturn" attribute; arguments as in
6793 struct attribute_spec.handler. */
6794
6795static tree
e18476eb
BI
6796handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6797 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6798{
6799 tree type = TREE_TYPE (*node);
6800
6801 /* See FIXME comment in c_common_attribute_table. */
2debdb4f
NP
6802 if (TREE_CODE (*node) == FUNCTION_DECL
6803 || objc_method_decl (TREE_CODE (*node)))
349ae713
NB
6804 TREE_THIS_VOLATILE (*node) = 1;
6805 else if (TREE_CODE (type) == POINTER_TYPE
6806 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6807 TREE_TYPE (*node)
6e7ceb17
PC
6808 = (build_qualified_type
6809 (build_pointer_type
6810 (build_type_variant (TREE_TYPE (type),
6811 TYPE_READONLY (TREE_TYPE (type)), 1)),
6812 TYPE_QUALS (type)));
349ae713
NB
6813 else
6814 {
5c498b10 6815 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6816 *no_add_attrs = true;
6817 }
6818
6819 return NULL_TREE;
6820}
6821
52bf96d2
JH
6822/* Handle a "hot" and attribute; arguments as in
6823 struct attribute_spec.handler. */
6824
6825static tree
6826handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
ab442df7 6827 int ARG_UNUSED (flags), bool *no_add_attrs)
52bf96d2 6828{
e45abe1f
RH
6829 if (TREE_CODE (*node) == FUNCTION_DECL
6830 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6831 {
6832 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6833 {
45484dcf
MP
6834 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6835 "with attribute %qs", name, "cold");
52bf96d2
JH
6836 *no_add_attrs = true;
6837 }
5779e713
MM
6838 /* Most of the rest of the hot processing is done later with
6839 lookup_attribute. */
52bf96d2
JH
6840 }
6841 else
6842 {
6843 warning (OPT_Wattributes, "%qE attribute ignored", name);
6844 *no_add_attrs = true;
6845 }
6846
6847 return NULL_TREE;
6848}
e45abe1f 6849
52bf96d2
JH
6850/* Handle a "cold" and attribute; arguments as in
6851 struct attribute_spec.handler. */
6852
6853static tree
6854handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6855 int ARG_UNUSED (flags), bool *no_add_attrs)
6856{
e45abe1f
RH
6857 if (TREE_CODE (*node) == FUNCTION_DECL
6858 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6859 {
6860 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6861 {
45484dcf
MP
6862 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6863 "with attribute %qs", name, "hot");
52bf96d2
JH
6864 *no_add_attrs = true;
6865 }
5779e713
MM
6866 /* Most of the rest of the cold processing is done later with
6867 lookup_attribute. */
52bf96d2
JH
6868 }
6869 else
6870 {
6871 warning (OPT_Wattributes, "%qE attribute ignored", name);
6872 *no_add_attrs = true;
6873 }
6874
6875 return NULL_TREE;
6876}
6877
e664c61c 6878/* Handle a "no_sanitize_address" attribute; arguments as in
77bc5132
JJ
6879 struct attribute_spec.handler. */
6880
6881static tree
e664c61c
KS
6882handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6883 bool *no_add_attrs)
77bc5132
JJ
6884{
6885 if (TREE_CODE (*node) != FUNCTION_DECL)
6886 {
6887 warning (OPT_Wattributes, "%qE attribute ignored", name);
6888 *no_add_attrs = true;
6889 }
6890
6891 return NULL_TREE;
6892}
6893
e664c61c
KS
6894/* Handle a "no_address_safety_analysis" attribute; arguments as in
6895 struct attribute_spec.handler. */
6896
6897static tree
6898handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6899 bool *no_add_attrs)
6900{
6901 if (TREE_CODE (*node) != FUNCTION_DECL)
6902 warning (OPT_Wattributes, "%qE attribute ignored", name);
6903 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6904 DECL_ATTRIBUTES (*node)
6905 = tree_cons (get_identifier ("no_sanitize_address"),
6906 NULL_TREE, DECL_ATTRIBUTES (*node));
6907 *no_add_attrs = true;
6908 return NULL_TREE;
6909}
6910
ce6923c5
MP
6911/* Handle a "no_sanitize_undefined" attribute; arguments as in
6912 struct attribute_spec.handler. */
6913
6914static tree
6915handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6916 bool *no_add_attrs)
6917{
6918 if (TREE_CODE (*node) != FUNCTION_DECL)
6919 {
6920 warning (OPT_Wattributes, "%qE attribute ignored", name);
6921 *no_add_attrs = true;
6922 }
6923
6924 return NULL_TREE;
6925}
6926
5434dc07
MD
6927/* Handle a "stack_protect" attribute; arguments as in
6928 struct attribute_spec.handler. */
6929static tree
6930handle_stack_protect_attribute (tree *node, tree name, tree, int,
6931 bool *no_add_attrs)
6932{
6933 if (TREE_CODE (*node) != FUNCTION_DECL)
6934 {
6935 warning (OPT_Wattributes, "%qE attribute ignored", name);
6936 *no_add_attrs = true;
6937 }
6938 else
6939 DECL_ATTRIBUTES (*node)
6940 = tree_cons (get_identifier ("stack_protect"),
6941 NULL_TREE, DECL_ATTRIBUTES (*node));
6942
6943 return NULL_TREE;
6944}
6945
349ae713
NB
6946/* Handle a "noinline" attribute; arguments as in
6947 struct attribute_spec.handler. */
6948
6949static tree
35b1a6fa 6950handle_noinline_attribute (tree *node, tree name,
e18476eb
BI
6951 tree ARG_UNUSED (args),
6952 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6953{
6954 if (TREE_CODE (*node) == FUNCTION_DECL)
45484dcf
MP
6955 {
6956 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6957 {
6958 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6959 "with attribute %qs", name, "always_inline");
6960 *no_add_attrs = true;
6961 }
6962 else
6963 DECL_UNINLINABLE (*node) = 1;
6964 }
349ae713
NB
6965 else
6966 {
5c498b10 6967 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6968 *no_add_attrs = true;
6969 }
6970
6971 return NULL_TREE;
6972}
6973
86631ea3
MJ
6974/* Handle a "noclone" attribute; arguments as in
6975 struct attribute_spec.handler. */
6976
6977static tree
6978handle_noclone_attribute (tree *node, tree name,
6979 tree ARG_UNUSED (args),
6980 int ARG_UNUSED (flags), bool *no_add_attrs)
6981{
6982 if (TREE_CODE (*node) != FUNCTION_DECL)
6983 {
6984 warning (OPT_Wattributes, "%qE attribute ignored", name);
6985 *no_add_attrs = true;
6986 }
6987
6988 return NULL_TREE;
6989}
6990
185c9e56
ML
6991/* Handle a "no_icf" attribute; arguments as in
6992 struct attribute_spec.handler. */
6993
6994static tree
6995handle_noicf_attribute (tree *node, tree name,
6996 tree ARG_UNUSED (args),
6997 int ARG_UNUSED (flags), bool *no_add_attrs)
6998{
6999 if (TREE_CODE (*node) != FUNCTION_DECL)
7000 {
7001 warning (OPT_Wattributes, "%qE attribute ignored", name);
7002 *no_add_attrs = true;
7003 }
7004
7005 return NULL_TREE;
7006}
7007
7008
349ae713
NB
7009/* Handle a "always_inline" attribute; arguments as in
7010 struct attribute_spec.handler. */
7011
7012static tree
35b1a6fa 7013handle_always_inline_attribute (tree *node, tree name,
e18476eb
BI
7014 tree ARG_UNUSED (args),
7015 int ARG_UNUSED (flags),
a742c759 7016 bool *no_add_attrs)
349ae713
NB
7017{
7018 if (TREE_CODE (*node) == FUNCTION_DECL)
7019 {
45484dcf
MP
7020 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7021 {
7022 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7023 "with %qs attribute", name, "noinline");
7024 *no_add_attrs = true;
7025 }
3b1661a9
ES
7026 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
7027 {
7028 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7029 "with %qs attribute", name, "target_clones");
7030 *no_add_attrs = true;
7031 }
45484dcf
MP
7032 else
7033 /* Set the attribute and mark it for disregarding inline
7034 limits. */
7035 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
349ae713
NB
7036 }
7037 else
7038 {
5c498b10 7039 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713 7040 *no_add_attrs = true;
4eb7fd83
JJ
7041 }
7042
7043 return NULL_TREE;
7044}
7045
7046/* Handle a "gnu_inline" attribute; arguments as in
7047 struct attribute_spec.handler. */
7048
7049static tree
7050handle_gnu_inline_attribute (tree *node, tree name,
7051 tree ARG_UNUSED (args),
7052 int ARG_UNUSED (flags),
7053 bool *no_add_attrs)
7054{
7055 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7056 {
7057 /* Do nothing else, just set the attribute. We'll get at
7058 it later with lookup_attribute. */
7059 }
7060 else
7061 {
7062 warning (OPT_Wattributes, "%qE attribute ignored", name);
7063 *no_add_attrs = true;
46a4da10
JH
7064 }
7065
7066 return NULL_TREE;
7067}
7068
7069/* Handle a "leaf" attribute; arguments as in
7070 struct attribute_spec.handler. */
7071
7072static tree
7073handle_leaf_attribute (tree *node, tree name,
7074 tree ARG_UNUSED (args),
7075 int ARG_UNUSED (flags), bool *no_add_attrs)
7076{
7077 if (TREE_CODE (*node) != FUNCTION_DECL)
7078 {
7079 warning (OPT_Wattributes, "%qE attribute ignored", name);
7080 *no_add_attrs = true;
7081 }
7082 if (!TREE_PUBLIC (*node))
7083 {
7084 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7085 *no_add_attrs = true;
349ae713
NB
7086 }
7087
7088 return NULL_TREE;
7089}
7090
d752cfdb
JJ
7091/* Handle an "artificial" attribute; arguments as in
7092 struct attribute_spec.handler. */
7093
7094static tree
7095handle_artificial_attribute (tree *node, tree name,
7096 tree ARG_UNUSED (args),
7097 int ARG_UNUSED (flags),
7098 bool *no_add_attrs)
7099{
7100 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7101 {
7102 /* Do nothing else, just set the attribute. We'll get at
7103 it later with lookup_attribute. */
7104 }
7105 else
7106 {
7107 warning (OPT_Wattributes, "%qE attribute ignored", name);
7108 *no_add_attrs = true;
7109 }
7110
7111 return NULL_TREE;
7112}
7113
0691d1d4
RG
7114/* Handle a "flatten" attribute; arguments as in
7115 struct attribute_spec.handler. */
7116
7117static tree
7118handle_flatten_attribute (tree *node, tree name,
c22cacf3
MS
7119 tree args ATTRIBUTE_UNUSED,
7120 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0691d1d4
RG
7121{
7122 if (TREE_CODE (*node) == FUNCTION_DECL)
7123 /* Do nothing else, just set the attribute. We'll get at
7124 it later with lookup_attribute. */
7125 ;
7126 else
7127 {
7128 warning (OPT_Wattributes, "%qE attribute ignored", name);
7129 *no_add_attrs = true;
7130 }
7131
7132 return NULL_TREE;
7133}
7134
d2af6a68
JJ
7135/* Handle a "warning" or "error" attribute; arguments as in
7136 struct attribute_spec.handler. */
7137
7138static tree
7139handle_error_attribute (tree *node, tree name, tree args,
7140 int ARG_UNUSED (flags), bool *no_add_attrs)
7141{
7142 if (TREE_CODE (*node) == FUNCTION_DECL
cf35e2b1 7143 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
d2af6a68
JJ
7144 /* Do nothing else, just set the attribute. We'll get at
7145 it later with lookup_attribute. */
7146 ;
7147 else
7148 {
7149 warning (OPT_Wattributes, "%qE attribute ignored", name);
7150 *no_add_attrs = true;
7151 }
7152
7153 return NULL_TREE;
7154}
0691d1d4 7155
349ae713
NB
7156/* Handle a "used" attribute; arguments as in
7157 struct attribute_spec.handler. */
7158
7159static tree
e18476eb
BI
7160handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7161 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7162{
d7ddbe24
RH
7163 tree node = *pnode;
7164
7165 if (TREE_CODE (node) == FUNCTION_DECL
0ae9bd27 7166 || (VAR_P (node) && TREE_STATIC (node))
3797cb21 7167 || (TREE_CODE (node) == TYPE_DECL))
4d7d0451 7168 {
4d7d0451 7169 TREE_USED (node) = 1;
8e3e233b 7170 DECL_PRESERVE_P (node) = 1;
0ae9bd27 7171 if (VAR_P (node))
ebfbbdc5 7172 DECL_READ_P (node) = 1;
4d7d0451 7173 }
349ae713
NB
7174 else
7175 {
5c498b10 7176 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7177 *no_add_attrs = true;
7178 }
7179
7180 return NULL_TREE;
7181}
7182
7183/* Handle a "unused" attribute; arguments as in
7184 struct attribute_spec.handler. */
7185
d067e05f 7186tree
e18476eb
BI
7187handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7188 int flags, bool *no_add_attrs)
349ae713
NB
7189{
7190 if (DECL_P (*node))
7191 {
7192 tree decl = *node;
7193
7194 if (TREE_CODE (decl) == PARM_DECL
af05e6e5 7195 || VAR_OR_FUNCTION_DECL_P (decl)
349ae713 7196 || TREE_CODE (decl) == LABEL_DECL
d067e05f 7197 || TREE_CODE (decl) == CONST_DECL
349ae713 7198 || TREE_CODE (decl) == TYPE_DECL)
ebfbbdc5
JJ
7199 {
7200 TREE_USED (decl) = 1;
0ae9bd27 7201 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
ebfbbdc5
JJ
7202 DECL_READ_P (decl) = 1;
7203 }
349ae713
NB
7204 else
7205 {
5c498b10 7206 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7207 *no_add_attrs = true;
7208 }
7209 }
7210 else
7211 {
7212 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 7213 *node = build_variant_type_copy (*node);
349ae713
NB
7214 TREE_USED (*node) = 1;
7215 }
7216
7217 return NULL_TREE;
7218}
7219
ce91e74c
JH
7220/* Handle a "externally_visible" attribute; arguments as in
7221 struct attribute_spec.handler. */
7222
7223static tree
7224handle_externally_visible_attribute (tree *pnode, tree name,
7225 tree ARG_UNUSED (args),
7226 int ARG_UNUSED (flags),
7227 bool *no_add_attrs)
7228{
7229 tree node = *pnode;
7230
21b634ae 7231 if (VAR_OR_FUNCTION_DECL_P (node))
ce91e74c 7232 {
343d4b27
JJ
7233 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7234 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7235 {
7236 warning (OPT_Wattributes,
7237 "%qE attribute have effect only on public objects", name);
7238 *no_add_attrs = true;
7239 }
ce91e74c 7240 }
ce91e74c
JH
7241 else
7242 {
7243 warning (OPT_Wattributes, "%qE attribute ignored", name);
7244 *no_add_attrs = true;
7245 }
7246
7247 return NULL_TREE;
7248}
7249
7861b648
AK
7250/* Handle the "no_reorder" attribute. Arguments as in
7251 struct attribute_spec.handler. */
7252
7253static tree
7254handle_no_reorder_attribute (tree *pnode,
7255 tree name,
7256 tree,
7257 int,
7258 bool *no_add_attrs)
7259{
7260 tree node = *pnode;
7261
21b634ae 7262 if (!VAR_OR_FUNCTION_DECL_P (node)
7861b648
AK
7263 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7264 {
7265 warning (OPT_Wattributes,
7266 "%qE attribute only affects top level objects",
7267 name);
7268 *no_add_attrs = true;
7269 }
7270
7271 return NULL_TREE;
7272}
7273
349ae713
NB
7274/* Handle a "const" attribute; arguments as in
7275 struct attribute_spec.handler. */
7276
7277static tree
e18476eb
BI
7278handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7279 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7280{
7281 tree type = TREE_TYPE (*node);
7282
7283 /* See FIXME comment on noreturn in c_common_attribute_table. */
7284 if (TREE_CODE (*node) == FUNCTION_DECL)
7285 TREE_READONLY (*node) = 1;
7286 else if (TREE_CODE (type) == POINTER_TYPE
7287 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7288 TREE_TYPE (*node)
6e7ceb17
PC
7289 = (build_qualified_type
7290 (build_pointer_type
7291 (build_type_variant (TREE_TYPE (type), 1,
7292 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7293 TYPE_QUALS (type)));
349ae713
NB
7294 else
7295 {
5c498b10 7296 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7297 *no_add_attrs = true;
7298 }
7299
7300 return NULL_TREE;
7301}
7302
ee45a32d
EB
7303/* Handle a "scalar_storage_order" attribute; arguments as in
7304 struct attribute_spec.handler. */
7305
7306static tree
7307handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7308 int flags, bool *no_add_attrs)
7309{
7310 tree id = TREE_VALUE (args);
7311 tree type;
7312
7313 if (TREE_CODE (*node) == TYPE_DECL
7314 && ! (flags & ATTR_FLAG_CXX11))
7315 node = &TREE_TYPE (*node);
7316 type = *node;
7317
7318 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7319 {
7320 error ("scalar_storage_order is not supported because endianness "
7321 "is not uniform");
7322 return NULL_TREE;
7323 }
7324
7325 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7326 {
7327 bool reverse = false;
7328
7329 if (TREE_CODE (id) == STRING_CST
7330 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7331 reverse = !BYTES_BIG_ENDIAN;
7332 else if (TREE_CODE (id) == STRING_CST
7333 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7334 reverse = BYTES_BIG_ENDIAN;
7335 else
7336 {
7337 error ("scalar_storage_order argument must be one of \"big-endian\""
7338 " or \"little-endian\"");
7339 return NULL_TREE;
7340 }
7341
7342 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7343 {
7344 if (reverse)
7345 /* A type variant isn't good enough, since we don't want a cast
7346 to such a type to be removed as a no-op. */
7347 *node = type = build_duplicate_type (type);
7348 }
7349
7350 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7351 return NULL_TREE;
7352 }
7353
7354 warning (OPT_Wattributes, "%qE attribute ignored", name);
7355 *no_add_attrs = true;
7356 return NULL_TREE;
7357}
7358
349ae713
NB
7359/* Handle a "transparent_union" attribute; arguments as in
7360 struct attribute_spec.handler. */
7361
7362static tree
35b1a6fa 7363handle_transparent_union_attribute (tree *node, tree name,
e18476eb 7364 tree ARG_UNUSED (args), int flags,
a742c759 7365 bool *no_add_attrs)
349ae713 7366{
4009f2e7 7367 tree type;
52dd234b
RH
7368
7369 *no_add_attrs = true;
349ae713 7370
e28d52cf
DS
7371 if (TREE_CODE (*node) == TYPE_DECL
7372 && ! (flags & ATTR_FLAG_CXX11))
4009f2e7
JM
7373 node = &TREE_TYPE (*node);
7374 type = *node;
349ae713 7375
52dd234b 7376 if (TREE_CODE (type) == UNION_TYPE)
349ae713 7377 {
d58d6eb5
JM
7378 /* Make sure that the first field will work for a transparent union.
7379 If the type isn't complete yet, leave the check to the code in
7380 finish_struct. */
7381 if (TYPE_SIZE (type))
7382 {
7383 tree first = first_field (type);
7384 if (first == NULL_TREE
7385 || DECL_ARTIFICIAL (first)
7386 || TYPE_MODE (type) != DECL_MODE (first))
7387 goto ignored;
7388 }
7389
349ae713 7390 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
52dd234b 7391 {
d58d6eb5
JM
7392 /* If the type isn't complete yet, setting the flag
7393 on a variant wouldn't ever be checked. */
7394 if (!TYPE_SIZE (type))
7395 goto ignored;
7396
7397 /* build_duplicate_type doesn't work for C++. */
7398 if (c_dialect_cxx ())
52dd234b
RH
7399 goto ignored;
7400
ee45a32d
EB
7401 /* A type variant isn't good enough, since we don't want a cast
7402 to such a type to be removed as a no-op. */
52dd234b
RH
7403 *node = type = build_duplicate_type (type);
7404 }
7405
75c8aac3
JH
7406 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7407 TYPE_TRANSPARENT_AGGR (t) = 1;
52dd234b 7408 return NULL_TREE;
349ae713
NB
7409 }
7410
52dd234b
RH
7411 ignored:
7412 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7413 return NULL_TREE;
7414}
7415
fc8600f9
MM
7416/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7417 get the requested priority for a constructor or destructor,
7418 possibly issuing diagnostics for invalid or reserved
7419 priorities. */
7420
7421static priority_type
7422get_priority (tree args, bool is_destructor)
7423{
7424 HOST_WIDE_INT pri;
b2f4bed8 7425 tree arg;
fc8600f9
MM
7426
7427 if (!args)
7428 return DEFAULT_INIT_PRIORITY;
b8698a0f 7429
f6fc5c86
MM
7430 if (!SUPPORTS_INIT_PRIORITY)
7431 {
7432 if (is_destructor)
7433 error ("destructor priorities are not supported");
7434 else
7435 error ("constructor priorities are not supported");
7436 return DEFAULT_INIT_PRIORITY;
7437 }
7438
b2f4bed8 7439 arg = TREE_VALUE (args);
fad7652e
JJ
7440 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7441 goto invalid;
7442 if (arg == error_mark_node)
7443 return DEFAULT_INIT_PRIORITY;
8d0d1915 7444 arg = default_conversion (arg);
9541ffee 7445 if (!tree_fits_shwi_p (arg)
b2f4bed8 7446 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
fc8600f9
MM
7447 goto invalid;
7448
9439e9a1 7449 pri = tree_to_shwi (arg);
fc8600f9
MM
7450 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7451 goto invalid;
7452
7453 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7454 {
7455 if (is_destructor)
7456 warning (0,
7457 "destructor priorities from 0 to %d are reserved "
b8698a0f 7458 "for the implementation",
fc8600f9
MM
7459 MAX_RESERVED_INIT_PRIORITY);
7460 else
7461 warning (0,
7462 "constructor priorities from 0 to %d are reserved "
b8698a0f 7463 "for the implementation",
fc8600f9
MM
7464 MAX_RESERVED_INIT_PRIORITY);
7465 }
7466 return pri;
7467
7468 invalid:
7469 if (is_destructor)
7470 error ("destructor priorities must be integers from 0 to %d inclusive",
7471 MAX_INIT_PRIORITY);
7472 else
7473 error ("constructor priorities must be integers from 0 to %d inclusive",
7474 MAX_INIT_PRIORITY);
7475 return DEFAULT_INIT_PRIORITY;
7476}
7477
349ae713
NB
7478/* Handle a "constructor" attribute; arguments as in
7479 struct attribute_spec.handler. */
7480
7481static tree
fc8600f9 7482handle_constructor_attribute (tree *node, tree name, tree args,
e18476eb 7483 int ARG_UNUSED (flags),
a742c759 7484 bool *no_add_attrs)
349ae713
NB
7485{
7486 tree decl = *node;
7487 tree type = TREE_TYPE (decl);
7488
7489 if (TREE_CODE (decl) == FUNCTION_DECL
7490 && TREE_CODE (type) == FUNCTION_TYPE
7491 && decl_function_context (decl) == 0)
7492 {
fc8600f9 7493 priority_type priority;
349ae713 7494 DECL_STATIC_CONSTRUCTOR (decl) = 1;
fc8600f9
MM
7495 priority = get_priority (args, /*is_destructor=*/false);
7496 SET_DECL_INIT_PRIORITY (decl, priority);
349ae713
NB
7497 TREE_USED (decl) = 1;
7498 }
7499 else
7500 {
5c498b10 7501 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7502 *no_add_attrs = true;
7503 }
7504
7505 return NULL_TREE;
7506}
7507
7508/* Handle a "destructor" attribute; arguments as in
7509 struct attribute_spec.handler. */
7510
7511static tree
fc8600f9 7512handle_destructor_attribute (tree *node, tree name, tree args,
e18476eb 7513 int ARG_UNUSED (flags),
a742c759 7514 bool *no_add_attrs)
349ae713
NB
7515{
7516 tree decl = *node;
7517 tree type = TREE_TYPE (decl);
7518
7519 if (TREE_CODE (decl) == FUNCTION_DECL
7520 && TREE_CODE (type) == FUNCTION_TYPE
7521 && decl_function_context (decl) == 0)
7522 {
fc8600f9 7523 priority_type priority;
349ae713 7524 DECL_STATIC_DESTRUCTOR (decl) = 1;
fc8600f9
MM
7525 priority = get_priority (args, /*is_destructor=*/true);
7526 SET_DECL_FINI_PRIORITY (decl, priority);
349ae713
NB
7527 TREE_USED (decl) = 1;
7528 }
7529 else
7530 {
5c498b10 7531 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7532 *no_add_attrs = true;
7533 }
7534
7535 return NULL_TREE;
7536}
7537
4d451982
MLI
7538/* Nonzero if the mode is a valid vector mode for this architecture.
7539 This returns nonzero even if there is no hardware support for the
7540 vector mode, but we can emulate with narrower modes. */
7541
7542static int
ef4bddc2 7543vector_mode_valid_p (machine_mode mode)
4d451982
MLI
7544{
7545 enum mode_class mclass = GET_MODE_CLASS (mode);
ef4bddc2 7546 machine_mode innermode;
4d451982
MLI
7547
7548 /* Doh! What's going on? */
7549 if (mclass != MODE_VECTOR_INT
7550 && mclass != MODE_VECTOR_FLOAT
7551 && mclass != MODE_VECTOR_FRACT
7552 && mclass != MODE_VECTOR_UFRACT
7553 && mclass != MODE_VECTOR_ACCUM
7554 && mclass != MODE_VECTOR_UACCUM)
7555 return 0;
7556
7557 /* Hardware support. Woo hoo! */
7558 if (targetm.vector_mode_supported_p (mode))
7559 return 1;
7560
7561 innermode = GET_MODE_INNER (mode);
7562
7563 /* We should probably return 1 if requesting V4DI and we have no DI,
7564 but we have V2DI, but this is probably very unlikely. */
7565
7566 /* If we have support for the inner mode, we can safely emulate it.
7567 We may not have V2DI, but me can emulate with a pair of DIs. */
7568 return targetm.scalar_mode_supported_p (innermode);
7569}
7570
7571
349ae713
NB
7572/* Handle a "mode" attribute; arguments as in
7573 struct attribute_spec.handler. */
7574
7575static tree
e18476eb
BI
7576handle_mode_attribute (tree *node, tree name, tree args,
7577 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7578{
7579 tree type = *node;
88388a52 7580 tree ident = TREE_VALUE (args);
349ae713
NB
7581
7582 *no_add_attrs = true;
7583
88388a52 7584 if (TREE_CODE (ident) != IDENTIFIER_NODE)
5c498b10 7585 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7586 else
7587 {
7588 int j;
88388a52 7589 const char *p = IDENTIFIER_POINTER (ident);
349ae713 7590 int len = strlen (p);
ef4bddc2 7591 machine_mode mode = VOIDmode;
349ae713 7592 tree typefm;
6dd53648 7593 bool valid_mode;
349ae713
NB
7594
7595 if (len > 4 && p[0] == '_' && p[1] == '_'
7596 && p[len - 1] == '_' && p[len - 2] == '_')
7597 {
28dab132 7598 char *newp = (char *) alloca (len - 1);
349ae713
NB
7599
7600 strcpy (newp, &p[2]);
7601 newp[len - 4] = '\0';
7602 p = newp;
7603 }
7604
7605 /* Change this type to have a type with the specified mode.
7606 First check for the special modes. */
3f75a254 7607 if (!strcmp (p, "byte"))
349ae713
NB
7608 mode = byte_mode;
7609 else if (!strcmp (p, "word"))
7610 mode = word_mode;
3f75a254 7611 else if (!strcmp (p, "pointer"))
349ae713 7612 mode = ptr_mode;
c7ff6e7a
AK
7613 else if (!strcmp (p, "libgcc_cmp_return"))
7614 mode = targetm.libgcc_cmp_return_mode ();
7615 else if (!strcmp (p, "libgcc_shift_count"))
7616 mode = targetm.libgcc_shift_count_mode ();
7b0518e3
UW
7617 else if (!strcmp (p, "unwind_word"))
7618 mode = targetm.unwind_word_mode ();
349ae713
NB
7619 else
7620 for (j = 0; j < NUM_MACHINE_MODES; j++)
7621 if (!strcmp (p, GET_MODE_NAME (j)))
61f03aba 7622 {
ef4bddc2 7623 mode = (machine_mode) j;
61f03aba
RH
7624 break;
7625 }
349ae713
NB
7626
7627 if (mode == VOIDmode)
4a5eab38 7628 {
88388a52 7629 error ("unknown machine mode %qE", ident);
4a5eab38
PB
7630 return NULL_TREE;
7631 }
7632
6dd53648
RH
7633 valid_mode = false;
7634 switch (GET_MODE_CLASS (mode))
4a5eab38 7635 {
6dd53648
RH
7636 case MODE_INT:
7637 case MODE_PARTIAL_INT:
7638 case MODE_FLOAT:
9a8ce21f 7639 case MODE_DECIMAL_FLOAT:
ab22c1fa
CF
7640 case MODE_FRACT:
7641 case MODE_UFRACT:
7642 case MODE_ACCUM:
7643 case MODE_UACCUM:
6dd53648
RH
7644 valid_mode = targetm.scalar_mode_supported_p (mode);
7645 break;
7646
7647 case MODE_COMPLEX_INT:
7648 case MODE_COMPLEX_FLOAT:
7649 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7650 break;
7651
7652 case MODE_VECTOR_INT:
7653 case MODE_VECTOR_FLOAT:
ab22c1fa
CF
7654 case MODE_VECTOR_FRACT:
7655 case MODE_VECTOR_UFRACT:
7656 case MODE_VECTOR_ACCUM:
7657 case MODE_VECTOR_UACCUM:
5c498b10
DD
7658 warning (OPT_Wattributes, "specifying vector types with "
7659 "__attribute__ ((mode)) is deprecated");
7660 warning (OPT_Wattributes,
7661 "use __attribute__ ((vector_size)) instead");
6dd53648
RH
7662 valid_mode = vector_mode_valid_p (mode);
7663 break;
4a5eab38 7664
6dd53648
RH
7665 default:
7666 break;
7667 }
7668 if (!valid_mode)
7669 {
9e637a26 7670 error ("unable to emulate %qs", p);
6dd53648
RH
7671 return NULL_TREE;
7672 }
4a5eab38 7673
6dd53648 7674 if (POINTER_TYPE_P (type))
cb2a532e 7675 {
36c5e70a 7676 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
ef4bddc2 7677 tree (*fn)(tree, machine_mode, bool);
6dd53648 7678
36c5e70a 7679 if (!targetm.addr_space.valid_pointer_mode (mode, as))
cb2a532e 7680 {
9e637a26 7681 error ("invalid pointer mode %qs", p);
cb2a532e
AH
7682 return NULL_TREE;
7683 }
7684
c22cacf3 7685 if (TREE_CODE (type) == POINTER_TYPE)
6dd53648 7686 fn = build_pointer_type_for_mode;
4977bab6 7687 else
6dd53648
RH
7688 fn = build_reference_type_for_mode;
7689 typefm = fn (TREE_TYPE (type), mode, false);
cb2a532e 7690 }
6dd53648 7691 else
ab22c1fa
CF
7692 {
7693 /* For fixed-point modes, we need to test if the signness of type
7694 and the machine mode are consistent. */
7695 if (ALL_FIXED_POINT_MODE_P (mode)
7696 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7697 {
d8a07487 7698 error ("signedness of type and machine mode %qs don%'t match", p);
ab22c1fa
CF
7699 return NULL_TREE;
7700 }
7701 /* For fixed-point modes, we need to pass saturating info. */
7702 typefm = lang_hooks.types.type_for_mode (mode,
7703 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7704 : TYPE_UNSIGNED (type));
7705 }
ec8465a5 7706
6dd53648
RH
7707 if (typefm == NULL_TREE)
7708 {
61f03aba 7709 error ("no data type for mode %qs", p);
6dd53648
RH
7710 return NULL_TREE;
7711 }
ec8465a5
RK
7712 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7713 {
7714 /* For enumeral types, copy the precision from the integer
7715 type returned above. If not an INTEGER_TYPE, we can't use
7716 this mode for this type. */
7717 if (TREE_CODE (typefm) != INTEGER_TYPE)
7718 {
61f03aba 7719 error ("cannot use mode %qs for enumeral types", p);
ec8465a5
RK
7720 return NULL_TREE;
7721 }
7722
99db1ef0
RH
7723 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7724 {
7725 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7726 typefm = type;
7727 }
7728 else
7729 {
7730 /* We cannot build a type variant, as there's code that assumes
7731 that TYPE_MAIN_VARIANT has the same mode. This includes the
7732 debug generators. Instead, create a subrange type. This
7733 results in all of the enumeral values being emitted only once
7734 in the original, and the subtype gets them by reference. */
7735 if (TYPE_UNSIGNED (type))
7736 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7737 else
7738 typefm = make_signed_type (TYPE_PRECISION (typefm));
7739 TREE_TYPE (typefm) = type;
7740 }
ec8465a5 7741 }
a2d36602
RH
7742 else if (VECTOR_MODE_P (mode)
7743 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7744 : TREE_CODE (type) != TREE_CODE (typefm))
61f03aba
RH
7745 {
7746 error ("mode %qs applied to inappropriate type", p);
7747 return NULL_TREE;
7748 }
7749
6dd53648 7750 *node = typefm;
349ae713
NB
7751 }
7752
7753 return NULL_TREE;
7754}
7755
7756/* Handle a "section" attribute; arguments as in
7757 struct attribute_spec.handler. */
7758
7759static tree
e18476eb
BI
7760handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7761 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7762{
7763 tree decl = *node;
7764
0373796b 7765 if (!targetm_common.have_named_sections)
349ae713 7766 {
0373796b
JT
7767 error_at (DECL_SOURCE_LOCATION (*node),
7768 "section attributes are not supported for this target");
7769 goto fail;
7770 }
9fb32434 7771
0373796b 7772 user_defined_section_attribute = true;
349ae713 7773
21b634ae 7774 if (!VAR_OR_FUNCTION_DECL_P (decl))
0373796b
JT
7775 {
7776 error ("section attribute not allowed for %q+D", *node);
7777 goto fail;
349ae713 7778 }
0373796b
JT
7779
7780 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
349ae713 7781 {
0373796b
JT
7782 error ("section attribute argument not a string constant");
7783 goto fail;
349ae713
NB
7784 }
7785
0ae9bd27 7786 if (VAR_P (decl)
0373796b
JT
7787 && current_function_decl != NULL_TREE
7788 && !TREE_STATIC (decl))
7789 {
7790 error_at (DECL_SOURCE_LOCATION (decl),
7791 "section attribute cannot be specified for local variables");
7792 goto fail;
7793 }
7794
7795 /* The decl may have already been given a section attribute
7796 from a previous declaration. Ensure they match. */
7797 if (DECL_SECTION_NAME (decl) != NULL
7798 && strcmp (DECL_SECTION_NAME (decl),
7799 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7800 {
7801 error ("section of %q+D conflicts with previous declaration", *node);
7802 goto fail;
7803 }
7804
0ae9bd27 7805 if (VAR_P (decl)
0373796b
JT
7806 && !targetm.have_tls && targetm.emutls.tmpl_section
7807 && DECL_THREAD_LOCAL_P (decl))
7808 {
7809 error ("section of %q+D cannot be overridden", *node);
7810 goto fail;
7811 }
7812
7813 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7814 return NULL_TREE;
7815
7816fail:
7817 *no_add_attrs = true;
349ae713
NB
7818 return NULL_TREE;
7819}
7820
d19fa6b5
JM
7821/* Check whether ALIGN is a valid user-specified alignment. If so,
7822 return its base-2 log; if not, output an error and return -1. If
7823 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7824 no error. */
7825int
7826check_user_alignment (const_tree align, bool allow_zero)
7827{
7828 int i;
7829
661a0813
MP
7830 if (error_operand_p (align))
7831 return -1;
a859517f
MP
7832 if (TREE_CODE (align) != INTEGER_CST
7833 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
d19fa6b5
JM
7834 {
7835 error ("requested alignment is not an integer constant");
7836 return -1;
7837 }
7838 else if (allow_zero && integer_zerop (align))
7839 return -1;
3f12f6e9
SKS
7840 else if (tree_int_cst_sgn (align) == -1
7841 || (i = tree_log2 (align)) == -1)
d19fa6b5 7842 {
3f12f6e9 7843 error ("requested alignment is not a positive power of 2");
d19fa6b5
JM
7844 return -1;
7845 }
ec1e2a40 7846 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
d19fa6b5
JM
7847 {
7848 error ("requested alignment is too large");
7849 return -1;
7850 }
7851 return i;
7852}
7853
e96809e3 7854/* If in c++-11, check if the c++-11 alignment constraint with respect
e28d52cf
DS
7855 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7856 c++-11 mode, does nothing.
7857
7858 [dcl.align]2/ says:
7859
7860 [* if the constant expression evaluates to a fundamental alignment,
7861 the alignment requirement of the declared entity shall be the
7862 specified fundamental alignment.
7863
7864 * if the constant expression evaluates to an extended alignment
7865 and the implementation supports that alignment in the context
7866 of the declaration, the alignment of the declared entity shall
7867 be that alignment
7868
7869 * if the constant expression evaluates to an extended alignment
7870 and the implementation does not support that alignment in the
7871 context of the declaration, the program is ill-formed]. */
7872
7873static bool
7874check_cxx_fundamental_alignment_constraints (tree node,
7875 unsigned align_log,
7876 int flags)
7877{
7878 bool alignment_too_large_p = false;
e96809e3 7879 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
e28d52cf
DS
7880 unsigned max_align = 0;
7881
7882 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7883 || (node == NULL_TREE || node == error_mark_node))
7884 return true;
7885
7886 if (cxx_fundamental_alignment_p (requested_alignment))
7887 return true;
7888
38711381 7889 if (VAR_P (node))
e28d52cf 7890 {
931388ce 7891 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
38711381
JM
7892 /* For file scope variables and static members, the target supports
7893 alignments that are at most MAX_OFILE_ALIGNMENT. */
7894 max_align = MAX_OFILE_ALIGNMENT;
e28d52cf 7895 else
38711381
JM
7896 /* For stack variables, the target supports at most
7897 MAX_STACK_ALIGNMENT. */
7898 max_align = MAX_STACK_ALIGNMENT;
7899 if (requested_alignment > max_align)
7900 alignment_too_large_p = true;
7901 }
7902 /* Let's be liberal for types and fields; don't limit their alignment any
7903 more than check_user_alignment already did. */
e28d52cf
DS
7904
7905 if (alignment_too_large_p)
7906 pedwarn (input_location, OPT_Wattributes,
7907 "requested alignment %d is larger than %d",
e96809e3 7908 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
e28d52cf
DS
7909
7910 return !alignment_too_large_p;
7911}
7912
349ae713
NB
7913/* Handle a "aligned" attribute; arguments as in
7914 struct attribute_spec.handler. */
7915
7916static tree
e18476eb 7917handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
a742c759 7918 int flags, bool *no_add_attrs)
349ae713
NB
7919{
7920 tree decl = NULL_TREE;
7921 tree *type = NULL;
7922 int is_type = 0;
5d77fb19 7923 tree align_expr;
349ae713
NB
7924 int i;
7925
5d77fb19
MG
7926 if (args)
7927 {
7928 align_expr = TREE_VALUE (args);
661a0813
MP
7929 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7930 && TREE_CODE (align_expr) != FUNCTION_DECL)
5d77fb19
MG
7931 align_expr = default_conversion (align_expr);
7932 }
7933 else
7934 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7935
349ae713
NB
7936 if (DECL_P (*node))
7937 {
7938 decl = *node;
7939 type = &TREE_TYPE (decl);
7940 is_type = TREE_CODE (*node) == TYPE_DECL;
7941 }
7942 else if (TYPE_P (*node))
7943 type = node, is_type = 1;
7944
509063eb 7945 if ((i = check_user_alignment (align_expr, true)) == -1
e28d52cf 7946 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
d19fa6b5 7947 *no_add_attrs = true;
349ae713
NB
7948 else if (is_type)
7949 {
0f559c16
JM
7950 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7951 /* OK, modify the type in place. */;
349ae713
NB
7952 /* If we have a TYPE_DECL, then copy the type, so that we
7953 don't accidentally modify a builtin type. See pushdecl. */
0f559c16
JM
7954 else if (decl && TREE_TYPE (decl) != error_mark_node
7955 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
349ae713
NB
7956 {
7957 tree tt = TREE_TYPE (decl);
8dd16ecc 7958 *type = build_variant_type_copy (*type);
349ae713
NB
7959 DECL_ORIGINAL_TYPE (decl) = tt;
7960 TYPE_NAME (*type) = decl;
7961 TREE_USED (*type) = TREE_USED (decl);
7962 TREE_TYPE (decl) = *type;
7963 }
0f559c16 7964 else
8dd16ecc 7965 *type = build_variant_type_copy (*type);
349ae713 7966
fe37c7af 7967 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
349ae713
NB
7968 TYPE_USER_ALIGN (*type) = 1;
7969 }
837edd5f 7970 else if (! VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
7971 && TREE_CODE (decl) != FIELD_DECL)
7972 {
dee15844 7973 error ("alignment may not be specified for %q+D", decl);
349ae713
NB
7974 *no_add_attrs = true;
7975 }
e28d52cf
DS
7976 else if (DECL_USER_ALIGN (decl)
7977 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7978 /* C++-11 [dcl.align/4]:
7979
7980 When multiple alignment-specifiers are specified for an
7981 entity, the alignment requirement shall be set to the
7982 strictest specified alignment.
7983
7984 This formally comes from the c++11 specification but we are
7985 doing it for the GNU attribute syntax as well. */
7986 *no_add_attrs = true;
837edd5f 7987 else if (TREE_CODE (decl) == FUNCTION_DECL
d9223014 7988 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
837edd5f
GK
7989 {
7990 if (DECL_USER_ALIGN (decl))
7991 error ("alignment for %q+D was previously specified as %d "
7992 "and may not be decreased", decl,
7993 DECL_ALIGN (decl) / BITS_PER_UNIT);
7994 else
7995 error ("alignment for %q+D must be at least %d", decl,
7996 DECL_ALIGN (decl) / BITS_PER_UNIT);
7997 *no_add_attrs = true;
7998 }
349ae713
NB
7999 else
8000 {
fe37c7af 8001 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
349ae713
NB
8002 DECL_USER_ALIGN (decl) = 1;
8003 }
8004
8005 return NULL_TREE;
8006}
8007
8008/* Handle a "weak" attribute; arguments as in
8009 struct attribute_spec.handler. */
8010
8011static tree
55af93a8 8012handle_weak_attribute (tree *node, tree name,
e18476eb
BI
8013 tree ARG_UNUSED (args),
8014 int ARG_UNUSED (flags),
8015 bool * ARG_UNUSED (no_add_attrs))
349ae713 8016{
55af93a8 8017 if (TREE_CODE (*node) == FUNCTION_DECL
6b4e94bc
RG
8018 && DECL_DECLARED_INLINE_P (*node))
8019 {
2aa9c6ae 8020 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6b4e94bc
RG
8021 *no_add_attrs = true;
8022 }
ba885ec5
NS
8023 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8024 {
8025 error ("indirect function %q+D cannot be declared weak", *node);
8026 *no_add_attrs = true;
8027 return NULL_TREE;
8028 }
21b634ae 8029 else if (VAR_OR_FUNCTION_DECL_P (*node))
c316b5e4 8030 declare_weak (*node);
55af93a8
DS
8031 else
8032 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8033
8034 return NULL_TREE;
8035}
8036
4bb794e2
ST
8037/* Handle a "noplt" attribute; arguments as in
8038 struct attribute_spec.handler. */
8039
8040static tree
8041handle_noplt_attribute (tree *node, tree name,
8042 tree ARG_UNUSED (args),
8043 int ARG_UNUSED (flags),
8044 bool * ARG_UNUSED (no_add_attrs))
8045{
8046 if (TREE_CODE (*node) != FUNCTION_DECL)
8047 {
8048 warning (OPT_Wattributes,
8049 "%qE attribute is only applicable on functions", name);
8050 *no_add_attrs = true;
8051 return NULL_TREE;
8052 }
8053 return NULL_TREE;
8054}
8055
ba885ec5
NS
8056/* Handle an "alias" or "ifunc" attribute; arguments as in
8057 struct attribute_spec.handler, except that IS_ALIAS tells us
8058 whether this is an alias as opposed to ifunc attribute. */
349ae713
NB
8059
8060static tree
ba885ec5
NS
8061handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8062 bool *no_add_attrs)
349ae713
NB
8063{
8064 tree decl = *node;
8065
ba885ec5 8066 if (TREE_CODE (decl) != FUNCTION_DECL
0ae9bd27 8067 && (!is_alias || !VAR_P (decl)))
feab5a67
JM
8068 {
8069 warning (OPT_Wattributes, "%qE attribute ignored", name);
8070 *no_add_attrs = true;
8071 }
8072 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
b8698a0f 8073 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825
GK
8074 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8075 /* A static variable declaration is always a tentative definition,
8076 but the alias is a non-tentative definition which overrides. */
b8698a0f 8077 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825 8078 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
349ae713 8079 {
ba885ec5 8080 error ("%q+D defined both normally and as %qE attribute", decl, name);
349ae713 8081 *no_add_attrs = true;
ba885ec5 8082 return NULL_TREE;
349ae713 8083 }
ba885ec5
NS
8084 else if (!is_alias
8085 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8086 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8087 {
8088 error ("weak %q+D cannot be defined %qE", decl, name);
8089 *no_add_attrs = true;
8090 return NULL_TREE;
8091 }
f6a76b9f
RH
8092
8093 /* Note that the very first time we process a nested declaration,
8094 decl_function_context will not be set. Indeed, *would* never
8095 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8096 we do below. After such frobbery, pushdecl would set the context.
8097 In any case, this is never what we want. */
8098 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
349ae713
NB
8099 {
8100 tree id;
8101
8102 id = TREE_VALUE (args);
8103 if (TREE_CODE (id) != STRING_CST)
8104 {
ba885ec5 8105 error ("attribute %qE argument not a string", name);
349ae713
NB
8106 *no_add_attrs = true;
8107 return NULL_TREE;
8108 }
8109 id = get_identifier (TREE_STRING_POINTER (id));
8110 /* This counts as a use of the object pointed to. */
8111 TREE_USED (id) = 1;
8112
8113 if (TREE_CODE (decl) == FUNCTION_DECL)
8114 DECL_INITIAL (decl) = error_mark_node;
8115 else
08346abd 8116 TREE_STATIC (decl) = 1;
ba885ec5
NS
8117
8118 if (!is_alias)
8119 /* ifuncs are also aliases, so set that attribute too. */
8120 DECL_ATTRIBUTES (decl)
8121 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
349ae713
NB
8122 }
8123 else
8124 {
5c498b10 8125 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8126 *no_add_attrs = true;
8127 }
8128
f7217cde
JH
8129 if (decl_in_symtab_p (*node))
8130 {
8131 struct symtab_node *n = symtab_node::get (decl);
8132 if (n && n->refuse_visibility_changes)
8133 {
8134 if (is_alias)
8135 error ("%+D declared alias after being used", decl);
8136 else
8137 error ("%+D declared ifunc after being used", decl);
8138 }
8139 }
8140
8141
349ae713
NB
8142 return NULL_TREE;
8143}
8144
ba885ec5
NS
8145/* Handle an "alias" or "ifunc" attribute; arguments as in
8146 struct attribute_spec.handler. */
8147
8148static tree
8149handle_ifunc_attribute (tree *node, tree name, tree args,
8150 int ARG_UNUSED (flags), bool *no_add_attrs)
8151{
8152 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8153}
8154
8155/* Handle an "alias" or "ifunc" attribute; arguments as in
8156 struct attribute_spec.handler. */
8157
8158static tree
8159handle_alias_attribute (tree *node, tree name, tree args,
8160 int ARG_UNUSED (flags), bool *no_add_attrs)
8161{
8162 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8163}
8164
a0203ca7
AO
8165/* Handle a "weakref" attribute; arguments as in struct
8166 attribute_spec.handler. */
8167
8168static tree
8169handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8170 int flags, bool *no_add_attrs)
8171{
8172 tree attr = NULL_TREE;
8173
e1cf56b1
AO
8174 /* We must ignore the attribute when it is associated with
8175 local-scoped decls, since attribute alias is ignored and many
8176 such symbols do not even have a DECL_WEAK field. */
e7b012c0
JJ
8177 if (decl_function_context (*node)
8178 || current_function_decl
21b634ae 8179 || !VAR_OR_FUNCTION_DECL_P (*node))
e1cf56b1
AO
8180 {
8181 warning (OPT_Wattributes, "%qE attribute ignored", name);
8182 *no_add_attrs = true;
8183 return NULL_TREE;
8184 }
8185
ba885ec5
NS
8186 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8187 {
8188 error ("indirect function %q+D cannot be declared weakref", *node);
8189 *no_add_attrs = true;
8190 return NULL_TREE;
8191 }
8192
a0203ca7
AO
8193 /* The idea here is that `weakref("name")' mutates into `weakref,
8194 alias("name")', and weakref without arguments, in turn,
8195 implicitly adds weak. */
8196
8197 if (args)
8198 {
8199 attr = tree_cons (get_identifier ("alias"), args, attr);
8200 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8201
8202 *no_add_attrs = true;
a9b0b825
GK
8203
8204 decl_attributes (node, attr, flags);
a0203ca7
AO
8205 }
8206 else
8207 {
8208 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
c5d75364
MLI
8209 error_at (DECL_SOURCE_LOCATION (*node),
8210 "weakref attribute must appear before alias attribute");
a0203ca7 8211
a9b0b825
GK
8212 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8213 and that isn't supported; and because it wants to add it to
8214 the list of weak decls, which isn't helpful. */
8215 DECL_WEAK (*node) = 1;
a0203ca7
AO
8216 }
8217
f7217cde
JH
8218 if (decl_in_symtab_p (*node))
8219 {
8220 struct symtab_node *n = symtab_node::get (*node);
8221 if (n && n->refuse_visibility_changes)
8222 error ("%+D declared weakref after being used", *node);
8223 }
8224
a0203ca7
AO
8225 return NULL_TREE;
8226}
8227
349ae713
NB
8228/* Handle an "visibility" attribute; arguments as in
8229 struct attribute_spec.handler. */
8230
8231static tree
35b1a6fa 8232handle_visibility_attribute (tree *node, tree name, tree args,
e18476eb 8233 int ARG_UNUSED (flags),
b9e75696 8234 bool *ARG_UNUSED (no_add_attrs))
349ae713
NB
8235{
8236 tree decl = *node;
968b41a1 8237 tree id = TREE_VALUE (args);
b9e75696 8238 enum symbol_visibility vis;
349ae713 8239
d7afec4b
ND
8240 if (TYPE_P (*node))
8241 {
b9e75696
JM
8242 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8243 /* OK */;
a868811e 8244 else if (!RECORD_OR_UNION_TYPE_P (*node))
b9e75696
JM
8245 {
8246 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8247 name);
8248 return NULL_TREE;
8249 }
8250 else if (TYPE_FIELDS (*node))
8251 {
8252 error ("%qE attribute ignored because %qT is already defined",
8253 name, *node);
8254 return NULL_TREE;
8255 }
d7afec4b 8256 }
3f75a254 8257 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
349ae713 8258 {
5c498b10 8259 warning (OPT_Wattributes, "%qE attribute ignored", name);
968b41a1 8260 return NULL_TREE;
349ae713 8261 }
349ae713 8262
968b41a1
MA
8263 if (TREE_CODE (id) != STRING_CST)
8264 {
40b97a2e 8265 error ("visibility argument not a string");
968b41a1 8266 return NULL_TREE;
349ae713 8267 }
9f63daea 8268
d7afec4b
ND
8269 /* If this is a type, set the visibility on the type decl. */
8270 if (TYPE_P (decl))
8271 {
8272 decl = TYPE_NAME (decl);
3f75a254 8273 if (!decl)
c22cacf3 8274 return NULL_TREE;
e8233ac2
AP
8275 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8276 {
5c498b10 8277 warning (OPT_Wattributes, "%qE attribute ignored on types",
e8233ac2
AP
8278 name);
8279 return NULL_TREE;
8280 }
d7afec4b 8281 }
349ae713 8282
968b41a1 8283 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
b9e75696 8284 vis = VISIBILITY_DEFAULT;
968b41a1 8285 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
b9e75696 8286 vis = VISIBILITY_INTERNAL;
968b41a1 8287 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
b9e75696 8288 vis = VISIBILITY_HIDDEN;
968b41a1 8289 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
b9e75696 8290 vis = VISIBILITY_PROTECTED;
968b41a1 8291 else
b9e75696
JM
8292 {
8293 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8294 vis = VISIBILITY_DEFAULT;
8295 }
8296
8297 if (DECL_VISIBILITY_SPECIFIED (decl)
3a687f8b
MM
8298 && vis != DECL_VISIBILITY (decl))
8299 {
8300 tree attributes = (TYPE_P (*node)
8301 ? TYPE_ATTRIBUTES (*node)
8302 : DECL_ATTRIBUTES (decl));
8303 if (lookup_attribute ("visibility", attributes))
8304 error ("%qD redeclared with different visibility", decl);
8305 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8306 && lookup_attribute ("dllimport", attributes))
8307 error ("%qD was declared %qs which implies default visibility",
8308 decl, "dllimport");
8309 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8310 && lookup_attribute ("dllexport", attributes))
8311 error ("%qD was declared %qs which implies default visibility",
8312 decl, "dllexport");
8313 }
b9e75696
JM
8314
8315 DECL_VISIBILITY (decl) = vis;
d7afec4b
ND
8316 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8317
b9e75696
JM
8318 /* Go ahead and attach the attribute to the node as well. This is needed
8319 so we can determine whether we have VISIBILITY_DEFAULT because the
8320 visibility was not specified, or because it was explicitly overridden
8321 from the containing scope. */
968b41a1 8322
349ae713
NB
8323 return NULL_TREE;
8324}
8325
b2ca3702
MM
8326/* Determine the ELF symbol visibility for DECL, which is either a
8327 variable or a function. It is an error to use this function if a
8328 definition of DECL is not available in this translation unit.
8329 Returns true if the final visibility has been determined by this
8330 function; false if the caller is free to make additional
8331 modifications. */
8332
8333bool
8334c_determine_visibility (tree decl)
8335{
21b634ae 8336 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
b2ca3702
MM
8337
8338 /* If the user explicitly specified the visibility with an
8339 attribute, honor that. DECL_VISIBILITY will have been set during
6d87092d
JM
8340 the processing of the attribute. We check for an explicit
8341 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8342 to distinguish the use of an attribute from the use of a "#pragma
8343 GCC visibility push(...)"; in the latter case we still want other
8344 considerations to be able to overrule the #pragma. */
8345 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8346 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8347 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8348 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
b2ca3702
MM
8349 return true;
8350
b9e75696
JM
8351 /* Set default visibility to whatever the user supplied with
8352 visibility_specified depending on #pragma GCC visibility. */
8353 if (!DECL_VISIBILITY_SPECIFIED (decl))
8354 {
09812622
JJ
8355 if (visibility_options.inpragma
8356 || DECL_VISIBILITY (decl) != default_visibility)
8357 {
8358 DECL_VISIBILITY (decl) = default_visibility;
8359 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8360 /* If visibility changed and DECL already has DECL_RTL, ensure
8361 symbol flags are updated. */
0ae9bd27 8362 if (((VAR_P (decl) && TREE_STATIC (decl))
09812622
JJ
8363 || TREE_CODE (decl) == FUNCTION_DECL)
8364 && DECL_RTL_SET_P (decl))
8365 make_decl_rtl (decl);
8366 }
b9e75696 8367 }
b2ca3702
MM
8368 return false;
8369}
8370
dce81a1a
JJ
8371/* Handle an "tls_model" attribute; arguments as in
8372 struct attribute_spec.handler. */
8373
8374static tree
35b1a6fa 8375handle_tls_model_attribute (tree *node, tree name, tree args,
e18476eb 8376 int ARG_UNUSED (flags), bool *no_add_attrs)
dce81a1a 8377{
c2f7fa15 8378 tree id;
dce81a1a 8379 tree decl = *node;
c2f7fa15 8380 enum tls_model kind;
dce81a1a 8381
c2f7fa15
SB
8382 *no_add_attrs = true;
8383
0ae9bd27 8384 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
dce81a1a 8385 {
5c498b10 8386 warning (OPT_Wattributes, "%qE attribute ignored", name);
c2f7fa15 8387 return NULL_TREE;
dce81a1a 8388 }
dce81a1a 8389
c2f7fa15
SB
8390 kind = DECL_TLS_MODEL (decl);
8391 id = TREE_VALUE (args);
8392 if (TREE_CODE (id) != STRING_CST)
8393 {
8394 error ("tls_model argument not a string");
8395 return NULL_TREE;
dce81a1a
JJ
8396 }
8397
c2f7fa15
SB
8398 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8399 kind = TLS_MODEL_LOCAL_EXEC;
8400 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8401 kind = TLS_MODEL_INITIAL_EXEC;
8402 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8403 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8404 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8405 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8406 else
8407 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8408
56363ffd 8409 set_decl_tls_model (decl, kind);
dce81a1a
JJ
8410 return NULL_TREE;
8411}
8412
349ae713
NB
8413/* Handle a "no_instrument_function" attribute; arguments as in
8414 struct attribute_spec.handler. */
8415
8416static tree
35b1a6fa 8417handle_no_instrument_function_attribute (tree *node, tree name,
e18476eb
BI
8418 tree ARG_UNUSED (args),
8419 int ARG_UNUSED (flags),
a742c759 8420 bool *no_add_attrs)
349ae713
NB
8421{
8422 tree decl = *node;
8423
8424 if (TREE_CODE (decl) != FUNCTION_DECL)
8425 {
c5d75364
MLI
8426 error_at (DECL_SOURCE_LOCATION (decl),
8427 "%qE attribute applies only to functions", name);
349ae713
NB
8428 *no_add_attrs = true;
8429 }
349ae713
NB
8430 else
8431 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8432
8433 return NULL_TREE;
8434}
8435
1225d6b1
ML
8436/* Handle a "no_profile_instrument_function" attribute; arguments as in
8437 struct attribute_spec.handler. */
8438
8439static tree
8440handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
8441 int, bool *no_add_attrs)
8442{
8443 if (TREE_CODE (*node) != FUNCTION_DECL)
8444 {
8445 warning (OPT_Wattributes, "%qE attribute ignored", name);
8446 *no_add_attrs = true;
8447 }
8448
8449 return NULL_TREE;
8450}
8451
349ae713
NB
8452/* Handle a "malloc" attribute; arguments as in
8453 struct attribute_spec.handler. */
8454
8455static tree
e18476eb
BI
8456handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8457 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 8458{
3425638a
JM
8459 if (TREE_CODE (*node) == FUNCTION_DECL
8460 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
349ae713 8461 DECL_IS_MALLOC (*node) = 1;
349ae713
NB
8462 else
8463 {
5c498b10 8464 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8465 *no_add_attrs = true;
8466 }
8467
8468 return NULL_TREE;
8469}
8470
51bc54a6
DM
8471/* Handle a "alloc_size" attribute; arguments as in
8472 struct attribute_spec.handler. */
8473
8474static tree
8475handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8476 int ARG_UNUSED (flags), bool *no_add_attrs)
8477{
f3f75f69 8478 unsigned arg_count = type_num_arguments (*node);
51bc54a6
DM
8479 for (; args; args = TREE_CHAIN (args))
8480 {
8481 tree position = TREE_VALUE (args);
5d77fb19
MG
8482 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8483 && TREE_CODE (position) != FUNCTION_DECL)
8484 position = default_conversion (position);
51bc54a6 8485
8fcbce72
JJ
8486 if (!tree_fits_uhwi_p (position)
8487 || !arg_count
8488 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
51bc54a6 8489 {
b8698a0f 8490 warning (OPT_Wattributes,
51bc54a6
DM
8491 "alloc_size parameter outside range");
8492 *no_add_attrs = true;
8493 return NULL_TREE;
8494 }
8495 }
8496 return NULL_TREE;
8497}
8498
8fcbce72
JJ
8499/* Handle a "alloc_align" attribute; arguments as in
8500 struct attribute_spec.handler. */
8501
8502static tree
8503handle_alloc_align_attribute (tree *node, tree, tree args, int,
8504 bool *no_add_attrs)
8505{
8506 unsigned arg_count = type_num_arguments (*node);
8507 tree position = TREE_VALUE (args);
78169471
MP
8508 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8509 && TREE_CODE (position) != FUNCTION_DECL)
8fcbce72
JJ
8510 position = default_conversion (position);
8511
8512 if (!tree_fits_uhwi_p (position)
8513 || !arg_count
8514 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8515 {
8516 warning (OPT_Wattributes,
8517 "alloc_align parameter outside range");
8518 *no_add_attrs = true;
8519 return NULL_TREE;
8520 }
8521 return NULL_TREE;
8522}
8523
8524/* Handle a "assume_aligned" attribute; arguments as in
8525 struct attribute_spec.handler. */
8526
8527static tree
8528handle_assume_aligned_attribute (tree *, tree, tree args, int,
8529 bool *no_add_attrs)
8530{
8531 for (; args; args = TREE_CHAIN (args))
8532 {
8533 tree position = TREE_VALUE (args);
8534 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8535 && TREE_CODE (position) != FUNCTION_DECL)
8536 position = default_conversion (position);
8537
8538 if (TREE_CODE (position) != INTEGER_CST)
8539 {
8540 warning (OPT_Wattributes,
8541 "assume_aligned parameter not integer constant");
8542 *no_add_attrs = true;
8543 return NULL_TREE;
8544 }
8545 }
8546 return NULL_TREE;
8547}
8548
0b7b376d
RG
8549/* Handle a "fn spec" attribute; arguments as in
8550 struct attribute_spec.handler. */
8551
8552static tree
8553handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8554 tree args, int ARG_UNUSED (flags),
8555 bool *no_add_attrs ATTRIBUTE_UNUSED)
8556{
8557 gcc_assert (args
8558 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8559 && !TREE_CHAIN (args));
8560 return NULL_TREE;
8561}
8562
d5e254e1
IE
8563/* Handle a "bnd_variable_size" attribute; arguments as in
8564 struct attribute_spec.handler. */
8565
8566static tree
8567handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8568 int ARG_UNUSED (flags), bool *no_add_attrs)
8569{
8570 if (TREE_CODE (*node) != FIELD_DECL)
8571 {
8572 warning (OPT_Wattributes, "%qE attribute ignored", name);
8573 *no_add_attrs = true;
8574 }
8575
8576 return NULL_TREE;
8577}
8578
8579/* Handle a "bnd_legacy" attribute; arguments as in
8580 struct attribute_spec.handler. */
8581
8582static tree
8583handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8584 int ARG_UNUSED (flags), bool *no_add_attrs)
8585{
8586 if (TREE_CODE (*node) != FUNCTION_DECL)
8587 {
8588 warning (OPT_Wattributes, "%qE attribute ignored", name);
8589 *no_add_attrs = true;
8590 }
8591
8592 return NULL_TREE;
8593}
8594
8595/* Handle a "bnd_instrument" attribute; arguments as in
8596 struct attribute_spec.handler. */
8597
8598static tree
8599handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8600 int ARG_UNUSED (flags), bool *no_add_attrs)
8601{
8602 if (TREE_CODE (*node) != FUNCTION_DECL)
8603 {
8604 warning (OPT_Wattributes, "%qE attribute ignored", name);
8605 *no_add_attrs = true;
8606 }
8607
8608 return NULL_TREE;
8609}
8610
2a99e5e6
LL
8611/* Handle a "warn_unused" attribute; arguments as in
8612 struct attribute_spec.handler. */
8613
8614static tree
8615handle_warn_unused_attribute (tree *node, tree name,
8616 tree args ATTRIBUTE_UNUSED,
8617 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8618{
8619 if (TYPE_P (*node))
8620 /* Do nothing else, just set the attribute. We'll get at
8621 it later with lookup_attribute. */
8622 ;
8623 else
8624 {
8625 warning (OPT_Wattributes, "%qE attribute ignored", name);
8626 *no_add_attrs = true;
8627 }
8628
8629 return NULL_TREE;
8630}
8631
acf0174b
JJ
8632/* Handle an "omp declare simd" attribute; arguments as in
8633 struct attribute_spec.handler. */
8634
8635static tree
8636handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8637{
8638 return NULL_TREE;
8639}
8640
fff77217
KY
8641/* Handle a "simd" attribute. */
8642
8643static tree
e7b69085 8644handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
fff77217
KY
8645{
8646 if (TREE_CODE (*node) == FUNCTION_DECL)
8647 {
8648 if (lookup_attribute ("cilk simd function",
8649 DECL_ATTRIBUTES (*node)) != NULL)
8650 {
8651 error_at (DECL_SOURCE_LOCATION (*node),
8652 "%<__simd__%> attribute cannot be used in the same "
8653 "function marked as a Cilk Plus SIMD-enabled function");
8654 *no_add_attrs = true;
8655 }
8656 else
e7b69085
KY
8657 {
8658 tree t = get_identifier ("omp declare simd");
8659 tree attr = NULL_TREE;
8660 if (args)
8661 {
8662 tree id = TREE_VALUE (args);
8663
8664 if (TREE_CODE (id) != STRING_CST)
8665 {
8666 error ("attribute %qE argument not a string", name);
8667 *no_add_attrs = true;
8668 return NULL_TREE;
8669 }
8670
8671 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8672 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8673 OMP_CLAUSE_NOTINBRANCH);
8674 else
8675 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8676 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8677 OMP_CLAUSE_INBRANCH);
8678 else
8679 {
8680 error ("only %<inbranch%> and %<notinbranch%> flags are "
8681 "allowed for %<__simd__%> attribute");
8682 *no_add_attrs = true;
8683 return NULL_TREE;
8684 }
8685 }
8686
8687 DECL_ATTRIBUTES (*node) = tree_cons (t,
8688 build_tree_list (NULL_TREE,
8689 attr),
8690 DECL_ATTRIBUTES (*node));
8691 }
fff77217
KY
8692 }
8693 else
8694 {
8695 warning (OPT_Wattributes, "%qE attribute ignored", name);
8696 *no_add_attrs = true;
8697 }
8698
8699 return NULL_TREE;
8700}
8701
acf0174b
JJ
8702/* Handle an "omp declare target" attribute; arguments as in
8703 struct attribute_spec.handler. */
8704
8705static tree
8706handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8707{
8708 return NULL_TREE;
8709}
8710
6e9a3221
AN
8711/* Handle a "returns_twice" attribute; arguments as in
8712 struct attribute_spec.handler. */
8713
8714static tree
8715handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8716 int ARG_UNUSED (flags), bool *no_add_attrs)
8717{
8718 if (TREE_CODE (*node) == FUNCTION_DECL)
8719 DECL_IS_RETURNS_TWICE (*node) = 1;
8720 else
8721 {
5c498b10 8722 warning (OPT_Wattributes, "%qE attribute ignored", name);
6e9a3221
AN
8723 *no_add_attrs = true;
8724 }
8725
8726 return NULL_TREE;
8727}
8728
349ae713
NB
8729/* Handle a "no_limit_stack" attribute; arguments as in
8730 struct attribute_spec.handler. */
8731
8732static tree
35b1a6fa 8733handle_no_limit_stack_attribute (tree *node, tree name,
e18476eb
BI
8734 tree ARG_UNUSED (args),
8735 int ARG_UNUSED (flags),
a742c759 8736 bool *no_add_attrs)
349ae713
NB
8737{
8738 tree decl = *node;
8739
8740 if (TREE_CODE (decl) != FUNCTION_DECL)
8741 {
c5d75364
MLI
8742 error_at (DECL_SOURCE_LOCATION (decl),
8743 "%qE attribute applies only to functions", name);
349ae713
NB
8744 *no_add_attrs = true;
8745 }
8746 else if (DECL_INITIAL (decl))
8747 {
c5d75364
MLI
8748 error_at (DECL_SOURCE_LOCATION (decl),
8749 "can%'t set %qE attribute after definition", name);
349ae713
NB
8750 *no_add_attrs = true;
8751 }
8752 else
8753 DECL_NO_LIMIT_STACK (decl) = 1;
8754
8755 return NULL_TREE;
8756}
8757
8758/* Handle a "pure" attribute; arguments as in
8759 struct attribute_spec.handler. */
8760
8761static tree
e18476eb
BI
8762handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8763 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
8764{
8765 if (TREE_CODE (*node) == FUNCTION_DECL)
becfd6e5 8766 DECL_PURE_P (*node) = 1;
349ae713
NB
8767 /* ??? TODO: Support types. */
8768 else
8769 {
5c498b10 8770 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8771 *no_add_attrs = true;
8772 }
8773
8774 return NULL_TREE;
8775}
8776
0a35513e
AH
8777/* Digest an attribute list destined for a transactional memory statement.
8778 ALLOWED is the set of attributes that are allowed for this statement;
8779 return the attribute we parsed. Multiple attributes are never allowed. */
8780
8781int
8782parse_tm_stmt_attr (tree attrs, int allowed)
8783{
8784 tree a_seen = NULL;
8785 int m_seen = 0;
8786
8787 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8788 {
8789 tree a = TREE_PURPOSE (attrs);
8790 int m = 0;
8791
8792 if (is_attribute_p ("outer", a))
8793 m = TM_STMT_ATTR_OUTER;
8794
8795 if ((m & allowed) == 0)
8796 {
8797 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8798 continue;
8799 }
8800
8801 if (m_seen == 0)
8802 {
8803 a_seen = a;
8804 m_seen = m;
8805 }
8806 else if (m_seen == m)
8807 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8808 else
8809 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8810 }
8811
8812 return m_seen;
8813}
8814
8815/* Transform a TM attribute name into a maskable integer and back.
8816 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8817 to how the lack of an attribute is treated. */
8818
8819int
8820tm_attr_to_mask (tree attr)
8821{
8822 if (attr == NULL)
8823 return 0;
8824 if (is_attribute_p ("transaction_safe", attr))
8825 return TM_ATTR_SAFE;
8826 if (is_attribute_p ("transaction_callable", attr))
8827 return TM_ATTR_CALLABLE;
8828 if (is_attribute_p ("transaction_pure", attr))
8829 return TM_ATTR_PURE;
8830 if (is_attribute_p ("transaction_unsafe", attr))
8831 return TM_ATTR_IRREVOCABLE;
8832 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8833 return TM_ATTR_MAY_CANCEL_OUTER;
8834 return 0;
8835}
8836
8837tree
8838tm_mask_to_attr (int mask)
8839{
8840 const char *str;
8841 switch (mask)
8842 {
8843 case TM_ATTR_SAFE:
8844 str = "transaction_safe";
8845 break;
8846 case TM_ATTR_CALLABLE:
8847 str = "transaction_callable";
8848 break;
8849 case TM_ATTR_PURE:
8850 str = "transaction_pure";
8851 break;
8852 case TM_ATTR_IRREVOCABLE:
8853 str = "transaction_unsafe";
8854 break;
8855 case TM_ATTR_MAY_CANCEL_OUTER:
8856 str = "transaction_may_cancel_outer";
8857 break;
8858 default:
8859 gcc_unreachable ();
8860 }
8861 return get_identifier (str);
8862}
8863
8864/* Return the first TM attribute seen in LIST. */
8865
8866tree
8867find_tm_attribute (tree list)
8868{
8869 for (; list ; list = TREE_CHAIN (list))
8870 {
8871 tree name = TREE_PURPOSE (list);
8872 if (tm_attr_to_mask (name) != 0)
8873 return name;
8874 }
8875 return NULL_TREE;
8876}
8877
8878/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8879 Here we accept only function types, and verify that none of the other
8880 function TM attributes are also applied. */
8881/* ??? We need to accept class types for C++, but not C. This greatly
8882 complicates this function, since we can no longer rely on the extra
8883 processing given by function_type_required. */
8884
8885static tree
8886handle_tm_attribute (tree *node, tree name, tree args,
8887 int flags, bool *no_add_attrs)
8888{
8889 /* Only one path adds the attribute; others don't. */
8890 *no_add_attrs = true;
8891
8892 switch (TREE_CODE (*node))
8893 {
8894 case RECORD_TYPE:
8895 case UNION_TYPE:
8896 /* Only tm_callable and tm_safe apply to classes. */
8897 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8898 goto ignored;
8899 /* FALLTHRU */
8900
8901 case FUNCTION_TYPE:
8902 case METHOD_TYPE:
8903 {
8904 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8905 if (old_name == name)
8906 ;
8907 else if (old_name != NULL_TREE)
8908 error ("type was previously declared %qE", old_name);
8909 else
8910 *no_add_attrs = false;
8911 }
8912 break;
8913
b8fd7909
JM
8914 case FUNCTION_DECL:
8915 {
8916 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8917 want to set transaction_safe on the type. */
8918 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8919 if (!TYPE_P (DECL_CONTEXT (*node)))
8920 error_at (DECL_SOURCE_LOCATION (*node),
8921 "%<transaction_safe_dynamic%> may only be specified for "
8922 "a virtual function");
8923 *no_add_attrs = false;
8924 decl_attributes (&TREE_TYPE (*node),
8925 build_tree_list (get_identifier ("transaction_safe"),
8926 NULL_TREE),
8927 0);
8928 break;
8929 }
8930
0a35513e
AH
8931 case POINTER_TYPE:
8932 {
8933 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8934 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8935 {
8936 tree fn_tmp = TREE_TYPE (*node);
8937 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8938 *node = build_pointer_type (fn_tmp);
8939 break;
8940 }
8941 }
8942 /* FALLTHRU */
8943
8944 default:
8945 /* If a function is next, pass it on to be tried next. */
8946 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8947 return tree_cons (name, args, NULL);
8948
8949 ignored:
8950 warning (OPT_Wattributes, "%qE attribute ignored", name);
8951 break;
8952 }
8953
8954 return NULL_TREE;
8955}
8956
8957/* Handle the TM_WRAP attribute; arguments as in
8958 struct attribute_spec.handler. */
8959
8960static tree
8961handle_tm_wrap_attribute (tree *node, tree name, tree args,
8962 int ARG_UNUSED (flags), bool *no_add_attrs)
8963{
8964 tree decl = *node;
8965
8966 /* We don't need the attribute even on success, since we
8967 record the entry in an external table. */
8968 *no_add_attrs = true;
8969
8970 if (TREE_CODE (decl) != FUNCTION_DECL)
8971 warning (OPT_Wattributes, "%qE attribute ignored", name);
8972 else
8973 {
8974 tree wrap_decl = TREE_VALUE (args);
661a0813
MP
8975 if (error_operand_p (wrap_decl))
8976 ;
8977 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
21b634ae 8978 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
0a35513e
AH
8979 error ("%qE argument not an identifier", name);
8980 else
8981 {
8982 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8983 wrap_decl = lookup_name (wrap_decl);
8984 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8985 {
8986 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8987 TREE_TYPE (wrap_decl)))
8988 record_tm_replacement (wrap_decl, decl);
8989 else
8990 error ("%qD is not compatible with %qD", wrap_decl, decl);
8991 }
8992 else
c07d7c02 8993 error ("%qE argument is not a function", name);
0a35513e
AH
8994 }
8995 }
8996
8997 return NULL_TREE;
8998}
8999
9000/* Ignore the given attribute. Used when this attribute may be usefully
9001 overridden by the target, but is not used generically. */
9002
9003static tree
9004ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9005 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9006 bool *no_add_attrs)
9007{
9008 *no_add_attrs = true;
9009 return NULL_TREE;
9010}
9011
dcd6de6d
ZD
9012/* Handle a "no vops" attribute; arguments as in
9013 struct attribute_spec.handler. */
9014
9015static tree
9016handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9017 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9018 bool *ARG_UNUSED (no_add_attrs))
9019{
9020 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9021 DECL_IS_NOVOPS (*node) = 1;
9022 return NULL_TREE;
9023}
9024
349ae713
NB
9025/* Handle a "deprecated" attribute; arguments as in
9026 struct attribute_spec.handler. */
35b1a6fa 9027
349ae713 9028static tree
35b1a6fa 9029handle_deprecated_attribute (tree *node, tree name,
9b86d6bb 9030 tree args, int flags,
a742c759 9031 bool *no_add_attrs)
349ae713
NB
9032{
9033 tree type = NULL_TREE;
9034 int warn = 0;
c51a1ba9 9035 tree what = NULL_TREE;
35b1a6fa 9036
9b86d6bb
L
9037 if (!args)
9038 *no_add_attrs = true;
9039 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9040 {
9041 error ("deprecated message is not a string");
9042 *no_add_attrs = true;
9043 }
9044
349ae713
NB
9045 if (DECL_P (*node))
9046 {
9047 tree decl = *node;
9048 type = TREE_TYPE (decl);
35b1a6fa 9049
349ae713
NB
9050 if (TREE_CODE (decl) == TYPE_DECL
9051 || TREE_CODE (decl) == PARM_DECL
21b634ae 9052 || VAR_OR_FUNCTION_DECL_P (decl)
a1178b30 9053 || TREE_CODE (decl) == FIELD_DECL
fd5c817a 9054 || TREE_CODE (decl) == CONST_DECL
a1178b30 9055 || objc_method_decl (TREE_CODE (decl)))
349ae713
NB
9056 TREE_DEPRECATED (decl) = 1;
9057 else
9058 warn = 1;
9059 }
9060 else if (TYPE_P (*node))
9061 {
9062 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 9063 *node = build_variant_type_copy (*node);
349ae713
NB
9064 TREE_DEPRECATED (*node) = 1;
9065 type = *node;
9066 }
9067 else
9068 warn = 1;
35b1a6fa 9069
349ae713
NB
9070 if (warn)
9071 {
9072 *no_add_attrs = true;
9073 if (type && TYPE_NAME (type))
9074 {
9075 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9 9076 what = TYPE_NAME (*node);
349ae713
NB
9077 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9078 && DECL_NAME (TYPE_NAME (type)))
c51a1ba9 9079 what = DECL_NAME (TYPE_NAME (type));
349ae713
NB
9080 }
9081 if (what)
5c498b10 9082 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
349ae713 9083 else
5c498b10 9084 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
9085 }
9086
9087 return NULL_TREE;
9088}
9089
349ae713
NB
9090/* Handle a "vector_size" attribute; arguments as in
9091 struct attribute_spec.handler. */
9092
9093static tree
35b1a6fa 9094handle_vector_size_attribute (tree *node, tree name, tree args,
e18476eb 9095 int ARG_UNUSED (flags),
a742c759 9096 bool *no_add_attrs)
349ae713
NB
9097{
9098 unsigned HOST_WIDE_INT vecsize, nunits;
ef4bddc2 9099 machine_mode orig_mode;
4a5eab38 9100 tree type = *node, new_type, size;
349ae713
NB
9101
9102 *no_add_attrs = true;
9103
4a5eab38 9104 size = TREE_VALUE (args);
661a0813
MP
9105 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9106 && TREE_CODE (size) != FUNCTION_DECL)
5d77fb19 9107 size = default_conversion (size);
4a5eab38 9108
cc269bb6 9109 if (!tree_fits_uhwi_p (size))
349ae713 9110 {
5c498b10 9111 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
9112 return NULL_TREE;
9113 }
9114
9115 /* Get the vector size (in bytes). */
ae7e9ddd 9116 vecsize = tree_to_uhwi (size);
349ae713
NB
9117
9118 /* We need to provide for vector pointers, vector arrays, and
9119 functions returning vectors. For example:
9120
9121 __attribute__((vector_size(16))) short *foo;
9122
9123 In this case, the mode is SI, but the type being modified is
9124 HI, so we need to look further. */
9125
9126 while (POINTER_TYPE_P (type)
9127 || TREE_CODE (type) == FUNCTION_TYPE
43dc123f 9128 || TREE_CODE (type) == METHOD_TYPE
270e749d
JJ
9129 || TREE_CODE (type) == ARRAY_TYPE
9130 || TREE_CODE (type) == OFFSET_TYPE)
349ae713
NB
9131 type = TREE_TYPE (type);
9132
9133 /* Get the mode of the type being modified. */
9134 orig_mode = TYPE_MODE (type);
9135
270e749d
JJ
9136 if ((!INTEGRAL_TYPE_P (type)
9137 && !SCALAR_FLOAT_TYPE_P (type)
9138 && !FIXED_POINT_TYPE_P (type))
3d8bf70f 9139 || (!SCALAR_FLOAT_MODE_P (orig_mode)
ab22c1fa
CF
9140 && GET_MODE_CLASS (orig_mode) != MODE_INT
9141 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
cc269bb6 9142 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
e4e5261f 9143 || TREE_CODE (type) == BOOLEAN_TYPE)
349ae713 9144 {
c51a1ba9 9145 error ("invalid vector type for attribute %qE", name);
349ae713
NB
9146 return NULL_TREE;
9147 }
9148
ae7e9ddd 9149 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
ee8960e5
JJ
9150 {
9151 error ("vector size not an integral multiple of component size");
9152 return NULL;
9153 }
9154
9155 if (vecsize == 0)
9156 {
9157 error ("zero vector size");
9158 return NULL;
9159 }
9160
349ae713 9161 /* Calculate how many units fit in the vector. */
ae7e9ddd 9162 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
26277d41 9163 if (nunits & (nunits - 1))
349ae713 9164 {
26277d41 9165 error ("number of components of the vector not a power of two");
349ae713
NB
9166 return NULL_TREE;
9167 }
9168
26277d41 9169 new_type = build_vector_type (type, nunits);
349ae713
NB
9170
9171 /* Build back pointers if needed. */
5dc11954 9172 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
349ae713
NB
9173
9174 return NULL_TREE;
9175}
9176
b34c7881
JT
9177/* Handle the "nonnull" attribute. */
9178static tree
e18476eb
BI
9179handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9180 tree args, int ARG_UNUSED (flags),
a742c759 9181 bool *no_add_attrs)
b34c7881
JT
9182{
9183 tree type = *node;
9184 unsigned HOST_WIDE_INT attr_arg_num;
9185
9186 /* If no arguments are specified, all pointer arguments should be
95bd1dd7 9187 non-null. Verify a full prototype is given so that the arguments
4d926e34
MS
9188 will have the correct types when we actually check them later.
9189 Avoid diagnosing type-generic built-ins since those have no
9190 prototype. */
3f75a254 9191 if (!args)
b34c7881 9192 {
4d926e34
MS
9193 if (!prototype_p (type)
9194 && (!TYPE_ATTRIBUTES (type)
9195 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
b34c7881
JT
9196 {
9197 error ("nonnull attribute without arguments on a non-prototype");
6de9cd9a 9198 *no_add_attrs = true;
b34c7881
JT
9199 }
9200 return NULL_TREE;
9201 }
9202
9203 /* Argument list specified. Verify that each argument number references
9204 a pointer argument. */
5d77fb19 9205 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
b34c7881 9206 {
6de9cd9a 9207 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
b34c7881 9208
5d77fb19
MG
9209 tree arg = TREE_VALUE (args);
9210 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9211 && TREE_CODE (arg) != FUNCTION_DECL)
9212 arg = default_conversion (arg);
9213
9214 if (!get_nonnull_operand (arg, &arg_num))
b34c7881 9215 {
40b97a2e 9216 error ("nonnull argument has invalid operand number (argument %lu)",
b34c7881
JT
9217 (unsigned long) attr_arg_num);
9218 *no_add_attrs = true;
9219 return NULL_TREE;
9220 }
9221
e19a18d4 9222 if (prototype_p (type))
b34c7881 9223 {
e19a18d4
NF
9224 function_args_iterator iter;
9225 tree argument;
9226
9227 function_args_iter_init (&iter, type);
9228 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
b34c7881 9229 {
e19a18d4
NF
9230 argument = function_args_iter_cond (&iter);
9231 if (argument == NULL_TREE || ck_num == arg_num)
b34c7881 9232 break;
b34c7881
JT
9233 }
9234
3f75a254 9235 if (!argument
e19a18d4 9236 || TREE_CODE (argument) == VOID_TYPE)
b34c7881 9237 {
40b97a2e 9238 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
b34c7881
JT
9239 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9240 *no_add_attrs = true;
9241 return NULL_TREE;
9242 }
9243
e19a18d4 9244 if (TREE_CODE (argument) != POINTER_TYPE)
b34c7881 9245 {
40b97a2e 9246 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
b34c7881
JT
9247 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9248 *no_add_attrs = true;
9249 return NULL_TREE;
9250 }
9251 }
9252 }
9253
9254 return NULL_TREE;
9255}
9256
9257/* Check the argument list of a function call for null in argument slots
94a0dd7b 9258 that are marked as requiring a non-null pointer argument. The NARGS
3342fd71 9259 arguments are passed in the array ARGARRAY. */
b34c7881
JT
9260
9261static void
3342fd71 9262check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
b34c7881 9263{
332f1d24 9264 tree a;
94a0dd7b 9265 int i;
b34c7881 9266
332f1d24
JJ
9267 attrs = lookup_attribute ("nonnull", attrs);
9268 if (attrs == NULL_TREE)
9269 return;
9270
9271 a = attrs;
9272 /* See if any of the nonnull attributes has no arguments. If so,
9273 then every pointer argument is checked (in which case the check
9274 for pointer type is done in check_nonnull_arg). */
9275 if (TREE_VALUE (a) != NULL_TREE)
9276 do
9277 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9278 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9279
9280 if (a != NULL_TREE)
9281 for (i = 0; i < nargs; i++)
3342fd71 9282 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
332f1d24
JJ
9283 i + 1);
9284 else
b34c7881 9285 {
332f1d24
JJ
9286 /* Walk the argument list. If we encounter an argument number we
9287 should check for non-null, do it. */
9288 for (i = 0; i < nargs; i++)
b34c7881 9289 {
332f1d24 9290 for (a = attrs; ; a = TREE_CHAIN (a))
6de9cd9a 9291 {
332f1d24
JJ
9292 a = lookup_attribute ("nonnull", a);
9293 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9294 break;
6de9cd9a 9295 }
332f1d24
JJ
9296
9297 if (a != NULL_TREE)
3342fd71 9298 check_function_arguments_recurse (check_nonnull_arg, &loc,
332f1d24 9299 argarray[i], i + 1);
b34c7881
JT
9300 }
9301 }
9302}
9303
254986c7 9304/* Check that the Nth argument of a function call (counting backwards
94a0dd7b
SL
9305 from the end) is a (pointer)0. The NARGS arguments are passed in the
9306 array ARGARRAY. */
3d091dac
KG
9307
9308static void
dde05067 9309check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
3d091dac 9310{
dde05067 9311 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
3d091dac
KG
9312
9313 if (attr)
9314 {
94a0dd7b
SL
9315 int len = 0;
9316 int pos = 0;
9317 tree sentinel;
dde05067
NF
9318 function_args_iterator iter;
9319 tree t;
c22cacf3 9320
94a0dd7b 9321 /* Skip over the named arguments. */
dde05067 9322 FOREACH_FUNCTION_ARGS (fntype, t, iter)
c22cacf3 9323 {
dde05067
NF
9324 if (len == nargs)
9325 break;
94a0dd7b
SL
9326 len++;
9327 }
254986c7 9328
94a0dd7b
SL
9329 if (TREE_VALUE (attr))
9330 {
9331 tree p = TREE_VALUE (TREE_VALUE (attr));
9332 pos = TREE_INT_CST_LOW (p);
9333 }
254986c7 9334
94a0dd7b
SL
9335 /* The sentinel must be one of the varargs, i.e.
9336 in position >= the number of fixed arguments. */
9337 if ((nargs - 1 - pos) < len)
9338 {
7332899a 9339 warning (OPT_Wformat_,
94a0dd7b
SL
9340 "not enough variable arguments to fit a sentinel");
9341 return;
3d091dac 9342 }
94a0dd7b
SL
9343
9344 /* Validate the sentinel. */
9345 sentinel = argarray[nargs - 1 - pos];
9346 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9347 || !integer_zerop (sentinel))
9348 /* Although __null (in C++) is only an integer we allow it
9349 nevertheless, as we are guaranteed that it's exactly
9350 as wide as a pointer, and we don't want to force
9351 users to cast the NULL they have written there.
9352 We warn with -Wstrict-null-sentinel, though. */
9353 && (warn_strict_null_sentinel || null_node != sentinel))
7332899a 9354 warning (OPT_Wformat_, "missing sentinel in function call");
3d091dac
KG
9355 }
9356}
9357
b34c7881
JT
9358/* Helper for check_function_nonnull; given a list of operands which
9359 must be non-null in ARGS, determine if operand PARAM_NUM should be
9360 checked. */
9361
9362static bool
35b1a6fa 9363nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
b34c7881 9364{
6de9cd9a 9365 unsigned HOST_WIDE_INT arg_num = 0;
b34c7881
JT
9366
9367 for (; args; args = TREE_CHAIN (args))
9368 {
366de0ce
NS
9369 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9370
9371 gcc_assert (found);
b34c7881
JT
9372
9373 if (arg_num == param_num)
9374 return true;
9375 }
9376 return false;
9377}
9378
9379/* Check that the function argument PARAM (which is operand number
9380 PARAM_NUM) is non-null. This is called by check_function_nonnull
9381 via check_function_arguments_recurse. */
9382
9383static void
3342fd71 9384check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
b34c7881 9385{
3342fd71
BS
9386 location_t *ploc = (location_t *) ctx;
9387
b34c7881
JT
9388 /* Just skip checking the argument if it's not a pointer. This can
9389 happen if the "nonnull" attribute was given without an operand
9390 list (which means to check every pointer argument). */
9391
9392 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9393 return;
9394
9395 if (integer_zerop (param))
3342fd71
BS
9396 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9397 "(argument %lu)", (unsigned long) param_num);
b34c7881
JT
9398}
9399
9400/* Helper for nonnull attribute handling; fetch the operand number
9401 from the attribute argument list. */
9402
9403static bool
35b1a6fa 9404get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
b34c7881 9405{
807e902e
KZ
9406 /* Verify the arg number is a small constant. */
9407 if (tree_fits_uhwi_p (arg_num_expr))
9408 {
9409 *valp = TREE_INT_CST_LOW (arg_num_expr);
9410 return true;
9411 }
9412 else
b34c7881 9413 return false;
b34c7881 9414}
39f2f3c8
RS
9415
9416/* Handle a "nothrow" attribute; arguments as in
9417 struct attribute_spec.handler. */
9418
9419static tree
e18476eb
BI
9420handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9421 int ARG_UNUSED (flags), bool *no_add_attrs)
39f2f3c8
RS
9422{
9423 if (TREE_CODE (*node) == FUNCTION_DECL)
9424 TREE_NOTHROW (*node) = 1;
9425 /* ??? TODO: Support types. */
9426 else
9427 {
5c498b10 9428 warning (OPT_Wattributes, "%qE attribute ignored", name);
39f2f3c8
RS
9429 *no_add_attrs = true;
9430 }
9431
9432 return NULL_TREE;
9433}
0bfa5f65
RH
9434
9435/* Handle a "cleanup" attribute; arguments as in
9436 struct attribute_spec.handler. */
9437
9438static tree
35b1a6fa 9439handle_cleanup_attribute (tree *node, tree name, tree args,
e18476eb 9440 int ARG_UNUSED (flags), bool *no_add_attrs)
0bfa5f65
RH
9441{
9442 tree decl = *node;
9443 tree cleanup_id, cleanup_decl;
9444
9445 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9446 for global destructors in C++. This requires infrastructure that
9447 we don't have generically at the moment. It's also not a feature
9448 we'd be missing too much, since we do have attribute constructor. */
0ae9bd27 9449 if (!VAR_P (decl) || TREE_STATIC (decl))
0bfa5f65 9450 {
5c498b10 9451 warning (OPT_Wattributes, "%qE attribute ignored", name);
0bfa5f65
RH
9452 *no_add_attrs = true;
9453 return NULL_TREE;
9454 }
9455
9456 /* Verify that the argument is a function in scope. */
9457 /* ??? We could support pointers to functions here as well, if
9458 that was considered desirable. */
9459 cleanup_id = TREE_VALUE (args);
9460 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9461 {
40b97a2e 9462 error ("cleanup argument not an identifier");
0bfa5f65
RH
9463 *no_add_attrs = true;
9464 return NULL_TREE;
9465 }
10e6657a 9466 cleanup_decl = lookup_name (cleanup_id);
0bfa5f65
RH
9467 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9468 {
40b97a2e 9469 error ("cleanup argument not a function");
0bfa5f65
RH
9470 *no_add_attrs = true;
9471 return NULL_TREE;
9472 }
9473
35b1a6fa 9474 /* That the function has proper type is checked with the
0bfa5f65
RH
9475 eventual call to build_function_call. */
9476
9477 return NULL_TREE;
9478}
72954a4f
JM
9479
9480/* Handle a "warn_unused_result" attribute. No special handling. */
9481
9482static tree
9483handle_warn_unused_result_attribute (tree *node, tree name,
e18476eb
BI
9484 tree ARG_UNUSED (args),
9485 int ARG_UNUSED (flags), bool *no_add_attrs)
72954a4f
JM
9486{
9487 /* Ignore the attribute for functions not returning any value. */
9488 if (VOID_TYPE_P (TREE_TYPE (*node)))
9489 {
5c498b10 9490 warning (OPT_Wattributes, "%qE attribute ignored", name);
72954a4f
JM
9491 *no_add_attrs = true;
9492 }
9493
9494 return NULL_TREE;
9495}
3d091dac
KG
9496
9497/* Handle a "sentinel" attribute. */
9498
9499static tree
254986c7 9500handle_sentinel_attribute (tree *node, tree name, tree args,
3d091dac
KG
9501 int ARG_UNUSED (flags), bool *no_add_attrs)
9502{
f4da8dce 9503 if (!prototype_p (*node))
3d091dac 9504 {
5c498b10
DD
9505 warning (OPT_Wattributes,
9506 "%qE attribute requires prototypes with named arguments", name);
3d091dac 9507 *no_add_attrs = true;
3d091dac 9508 }
254986c7
KG
9509 else
9510 {
dcf0c47e 9511 if (!stdarg_p (*node))
c22cacf3 9512 {
5c498b10
DD
9513 warning (OPT_Wattributes,
9514 "%qE attribute only applies to variadic functions", name);
254986c7
KG
9515 *no_add_attrs = true;
9516 }
9517 }
c22cacf3 9518
254986c7 9519 if (args)
3d091dac 9520 {
254986c7 9521 tree position = TREE_VALUE (args);
661a0813
MP
9522 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9523 && TREE_CODE (position) != FUNCTION_DECL)
9524 position = default_conversion (position);
254986c7 9525
661a0813
MP
9526 if (TREE_CODE (position) != INTEGER_CST
9527 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
c22cacf3 9528 {
b8698a0f 9529 warning (OPT_Wattributes,
aa86a51b 9530 "requested position is not an integer constant");
254986c7
KG
9531 *no_add_attrs = true;
9532 }
9533 else
c22cacf3 9534 {
254986c7
KG
9535 if (tree_int_cst_lt (position, integer_zero_node))
9536 {
aa86a51b
DM
9537 warning (OPT_Wattributes,
9538 "requested position is less than zero");
254986c7
KG
9539 *no_add_attrs = true;
9540 }
9541 }
3d091dac 9542 }
c22cacf3 9543
3d091dac
KG
9544 return NULL_TREE;
9545}
b5d32c25
KG
9546
9547/* Handle a "type_generic" attribute. */
9548
9549static tree
9550handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9551 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9552 bool * ARG_UNUSED (no_add_attrs))
9553{
3bf5906b
KG
9554 /* Ensure we have a function type. */
9555 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
b8698a0f 9556
3bf5906b 9557 /* Ensure we have a variadic function. */
dcf0c47e 9558 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5d32c25
KG
9559
9560 return NULL_TREE;
9561}
ab442df7 9562
5779e713 9563/* Handle a "target" attribute. */
ab442df7
MM
9564
9565static tree
5779e713 9566handle_target_attribute (tree *node, tree name, tree args, int flags,
ab442df7
MM
9567 bool *no_add_attrs)
9568{
9569 /* Ensure we have a function type. */
9570 if (TREE_CODE (*node) != FUNCTION_DECL)
9571 {
9572 warning (OPT_Wattributes, "%qE attribute ignored", name);
9573 *no_add_attrs = true;
9574 }
3b1661a9
ES
9575 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9576 {
9577 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9578 "with %qs attribute", name, "target_clones");
9579 *no_add_attrs = true;
9580 }
ab442df7 9581 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
32887460 9582 flags))
ab442df7
MM
9583 *no_add_attrs = true;
9584
9585 return NULL_TREE;
9586}
9587
3b1661a9
ES
9588/* Handle a "target_clones" attribute. */
9589
9590static tree
9591handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9592 int ARG_UNUSED (flags), bool *no_add_attrs)
9593{
9594 /* Ensure we have a function type. */
9595 if (TREE_CODE (*node) == FUNCTION_DECL)
9596 {
9597 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9598 {
9599 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9600 "with %qs attribute", name, "always_inline");
9601 *no_add_attrs = true;
9602 }
9603 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9604 {
9605 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9606 "with %qs attribute", name, "target");
9607 *no_add_attrs = true;
9608 }
9609 else
9610 /* Do not inline functions with multiple clone targets. */
9611 DECL_UNINLINABLE (*node) = 1;
9612 }
9613 else
9614 {
9615 warning (OPT_Wattributes, "%qE attribute ignored", name);
9616 *no_add_attrs = true;
9617 }
9618 return NULL_TREE;
9619}
9620
ab442df7
MM
9621/* Arguments being collected for optimization. */
9622typedef const char *const_char_p; /* For DEF_VEC_P. */
9771b263 9623static GTY(()) vec<const_char_p, va_gc> *optimize_args;
ab442df7
MM
9624
9625
9626/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9627 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9628 false for #pragma GCC optimize. */
9629
9630bool
9631parse_optimize_options (tree args, bool attr_p)
9632{
9633 bool ret = true;
9634 unsigned opt_argc;
9635 unsigned i;
9636 const char **opt_argv;
6e2f1956
JM
9637 struct cl_decoded_option *decoded_options;
9638 unsigned int decoded_options_count;
ab442df7
MM
9639 tree ap;
9640
9641 /* Build up argv vector. Just in case the string is stored away, use garbage
9642 collected strings. */
9771b263
DN
9643 vec_safe_truncate (optimize_args, 0);
9644 vec_safe_push (optimize_args, (const char *) NULL);
ab442df7
MM
9645
9646 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9647 {
9648 tree value = TREE_VALUE (ap);
9649
9650 if (TREE_CODE (value) == INTEGER_CST)
9651 {
9652 char buffer[20];
9653 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9771b263 9654 vec_safe_push (optimize_args, ggc_strdup (buffer));
ab442df7
MM
9655 }
9656
9657 else if (TREE_CODE (value) == STRING_CST)
9658 {
9659 /* Split string into multiple substrings. */
9660 size_t len = TREE_STRING_LENGTH (value);
9661 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9662 char *end = p + len;
9663 char *comma;
9664 char *next_p = p;
9665
9666 while (next_p != NULL)
9667 {
9668 size_t len2;
9669 char *q, *r;
9670
9671 p = next_p;
9672 comma = strchr (p, ',');
9673 if (comma)
9674 {
9675 len2 = comma - p;
9676 *comma = '\0';
9677 next_p = comma+1;
9678 }
9679 else
9680 {
9681 len2 = end - p;
9682 next_p = NULL;
9683 }
9684
a9429e29 9685 r = q = (char *) ggc_alloc_atomic (len2 + 3);
ab442df7
MM
9686
9687 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9688 options. */
9689 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9690 {
9691 ret = false;
9692 if (attr_p)
9693 warning (OPT_Wattributes,
a80a7051 9694 "bad option %qs to attribute %<optimize%>", p);
ab442df7
MM
9695 else
9696 warning (OPT_Wpragmas,
a80a7051 9697 "bad option %qs to pragma %<optimize%>", p);
ab442df7
MM
9698 continue;
9699 }
9700
9701 if (*p != '-')
9702 {
9703 *r++ = '-';
9704
9705 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9706 itself is -Os, and any other switch begins with a -f. */
9707 if ((*p >= '0' && *p <= '9')
9708 || (p[0] == 's' && p[1] == '\0'))
9709 *r++ = 'O';
9710 else if (*p != 'O')
9711 *r++ = 'f';
9712 }
9713
9714 memcpy (r, p, len2);
9715 r[len2] = '\0';
9771b263 9716 vec_safe_push (optimize_args, (const char *) q);
ab442df7
MM
9717 }
9718
9719 }
9720 }
9721
9771b263 9722 opt_argc = optimize_args->length ();
ab442df7
MM
9723 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9724
9725 for (i = 1; i < opt_argc; i++)
9771b263 9726 opt_argv[i] = (*optimize_args)[i];
ab442df7
MM
9727
9728 /* Now parse the options. */
a75bfaa6
JM
9729 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9730 &decoded_options,
9731 &decoded_options_count);
bfd67b47
RB
9732 /* Drop non-Optimization options. */
9733 unsigned j = 1;
9734 for (i = 1; i < decoded_options_count; ++i)
9735 {
9736 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
9737 {
9738 ret = false;
9739 if (attr_p)
9740 warning (OPT_Wattributes,
a80a7051 9741 "bad option %qs to attribute %<optimize%>",
bfd67b47
RB
9742 decoded_options[i].orig_option_with_args_text);
9743 else
9744 warning (OPT_Wpragmas,
a80a7051 9745 "bad option %qs to pragma %<optimize%>",
bfd67b47
RB
9746 decoded_options[i].orig_option_with_args_text);
9747 continue;
9748 }
9749 if (i != j)
9750 decoded_options[j] = decoded_options[i];
9751 j++;
9752 }
9753 decoded_options_count = j;
9754 /* And apply them. */
a75bfaa6 9755 decode_options (&global_options, &global_options_set,
a4d8c676
JM
9756 decoded_options, decoded_options_count,
9757 input_location, global_dc);
ab442df7 9758
2b7e2984
SE
9759 targetm.override_options_after_change();
9760
9771b263 9761 optimize_args->truncate (0);
ab442df7
MM
9762 return ret;
9763}
9764
9765/* For handling "optimize" attribute. arguments as in
9766 struct attribute_spec.handler. */
9767
9768static tree
9769handle_optimize_attribute (tree *node, tree name, tree args,
9770 int ARG_UNUSED (flags), bool *no_add_attrs)
9771{
9772 /* Ensure we have a function type. */
9773 if (TREE_CODE (*node) != FUNCTION_DECL)
9774 {
9775 warning (OPT_Wattributes, "%qE attribute ignored", name);
9776 *no_add_attrs = true;
9777 }
9778 else
9779 {
9780 struct cl_optimization cur_opts;
9781 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9782
9783 /* Save current options. */
46625112 9784 cl_optimization_save (&cur_opts, &global_options);
ab442df7
MM
9785
9786 /* If we previously had some optimization options, use them as the
9787 default. */
9788 if (old_opts)
46625112
JM
9789 cl_optimization_restore (&global_options,
9790 TREE_OPTIMIZATION (old_opts));
ab442df7
MM
9791
9792 /* Parse options, and update the vector. */
9793 parse_optimize_options (args, true);
9794 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
bf7b5747 9795 = build_optimization_node (&global_options);
ab442df7
MM
9796
9797 /* Restore current options. */
46625112 9798 cl_optimization_restore (&global_options, &cur_opts);
ab442df7
MM
9799 }
9800
9801 return NULL_TREE;
9802}
7458026b
ILT
9803
9804/* Handle a "no_split_stack" attribute. */
9805
9806static tree
9807handle_no_split_stack_attribute (tree *node, tree name,
9808 tree ARG_UNUSED (args),
9809 int ARG_UNUSED (flags),
9810 bool *no_add_attrs)
9811{
9812 tree decl = *node;
9813
9814 if (TREE_CODE (decl) != FUNCTION_DECL)
9815 {
9816 error_at (DECL_SOURCE_LOCATION (decl),
9817 "%qE attribute applies only to functions", name);
9818 *no_add_attrs = true;
9819 }
9820 else if (DECL_INITIAL (decl))
9821 {
9822 error_at (DECL_SOURCE_LOCATION (decl),
9823 "can%'t set %qE attribute after definition", name);
9824 *no_add_attrs = true;
9825 }
9826
9827 return NULL_TREE;
9828}
826cacfe
MG
9829
9830/* Handle a "returns_nonnull" attribute; arguments as in
9831 struct attribute_spec.handler. */
9832
9833static tree
9834handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9835 bool *no_add_attrs)
9836{
9837 // Even without a prototype we still have a return type we can check.
9838 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9839 {
9840 error ("returns_nonnull attribute on a function not returning a pointer");
9841 *no_add_attrs = true;
9842 }
9843 return NULL_TREE;
9844}
9845
976d5a22
TT
9846/* Handle a "designated_init" attribute; arguments as in
9847 struct attribute_spec.handler. */
9848
9849static tree
9850handle_designated_init_attribute (tree *node, tree name, tree, int,
9851 bool *no_add_attrs)
9852{
9853 if (TREE_CODE (*node) != RECORD_TYPE)
9854 {
9855 error ("%qE attribute is only valid on %<struct%> type", name);
9856 *no_add_attrs = true;
9857 }
9858 return NULL_TREE;
9859}
9860
81fea426
MP
9861
9862/* Handle a "fallthrough" attribute; arguments as in struct
9863 attribute_spec.handler. */
9864
9865static tree
9866handle_fallthrough_attribute (tree *, tree name, tree, int,
9867 bool *no_add_attrs)
9868{
9869 warning (OPT_Wattributes, "%qE attribute ignored", name);
9870 *no_add_attrs = true;
9871 return NULL_TREE;
9872}
9873
9874/* Check whether ATTR is a valid attribute fallthrough. */
9875
9876bool
9877attribute_fallthrough_p (tree attr)
9878{
9879 tree t = lookup_attribute ("fallthrough", attr);
9880 if (t == NULL_TREE)
9881 return false;
9882 /* This attribute shall appear at most once in each attribute-list. */
9883 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
9884 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
9885 "times");
9886 /* No attribute-argument-clause shall be present. */
9887 else if (TREE_VALUE (t) != NULL_TREE)
9888 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
9889 "a parameter");
9890 /* Warn if other attributes are found. */
9891 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
9892 {
9893 tree name = get_attribute_name (t);
9894 if (!is_attribute_p ("fallthrough", name))
9895 warning (OPT_Wattributes, "%qE attribute ignored", name);
9896 }
9897 return true;
9898}
9899
b34c7881 9900\f
dde05067 9901/* Check for valid arguments being passed to a function with FNTYPE.
3342fd71
BS
9902 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9903 diagnostics. */
b34c7881 9904void
3342fd71
BS
9905check_function_arguments (location_t loc, const_tree fntype, int nargs,
9906 tree *argarray)
b34c7881
JT
9907{
9908 /* Check for null being passed in a pointer argument that must be
9909 non-null. We also need to do this if format checking is enabled. */
9910
9911 if (warn_nonnull)
3342fd71 9912 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
b34c7881
JT
9913
9914 /* Check for errors in format strings. */
9915
e6c69da0 9916 if (warn_format || warn_suggest_attribute_format)
dde05067 9917 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7876a414
KG
9918
9919 if (warn_format)
dde05067 9920 check_function_sentinel (fntype, nargs, argarray);
b34c7881
JT
9921}
9922
9923/* Generic argument checking recursion routine. PARAM is the argument to
9924 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9925 once the argument is resolved. CTX is context for the callback. */
9926void
35b1a6fa
AJ
9927check_function_arguments_recurse (void (*callback)
9928 (void *, tree, unsigned HOST_WIDE_INT),
9929 void *ctx, tree param,
9930 unsigned HOST_WIDE_INT param_num)
b34c7881 9931{
1043771b 9932 if (CONVERT_EXPR_P (param)
1344f9a3
JM
9933 && (TYPE_PRECISION (TREE_TYPE (param))
9934 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
9935 {
9936 /* Strip coercion. */
9937 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9938 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
9939 return;
9940 }
9941
9942 if (TREE_CODE (param) == CALL_EXPR)
9943 {
5039610b 9944 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
9945 tree attrs;
9946 bool found_format_arg = false;
9947
9948 /* See if this is a call to a known internationalization function
9949 that modifies a format arg. Such a function may have multiple
9950 format_arg attributes (for example, ngettext). */
9951
9952 for (attrs = TYPE_ATTRIBUTES (type);
9953 attrs;
9954 attrs = TREE_CHAIN (attrs))
9955 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9956 {
5039610b 9957 tree inner_arg;
b34c7881
JT
9958 tree format_num_expr;
9959 int format_num;
9960 int i;
5039610b 9961 call_expr_arg_iterator iter;
b34c7881
JT
9962
9963 /* Extract the argument number, which was previously checked
9964 to be valid. */
9965 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 9966
6b3b8c27 9967 format_num = tree_to_uhwi (format_num_expr);
b34c7881 9968
5039610b
SL
9969 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9970 inner_arg != 0;
9971 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
9972 if (i == format_num)
9973 {
9974 check_function_arguments_recurse (callback, ctx,
5039610b 9975 inner_arg, param_num);
b34c7881
JT
9976 found_format_arg = true;
9977 break;
9978 }
9979 }
9980
9981 /* If we found a format_arg attribute and did a recursive check,
9982 we are done with checking this argument. Otherwise, we continue
9983 and this will be considered a non-literal. */
9984 if (found_format_arg)
9985 return;
9986 }
9987
9988 if (TREE_CODE (param) == COND_EXPR)
9989 {
7f26f7df
JM
9990 /* Simplify to avoid warning for an impossible case. */
9991 param = fold_for_warn (param);
9992 if (TREE_CODE (param) == COND_EXPR)
9993 {
9994 /* Check both halves of the conditional expression. */
9995 check_function_arguments_recurse (callback, ctx,
9996 TREE_OPERAND (param, 1),
9997 param_num);
9998 check_function_arguments_recurse (callback, ctx,
9999 TREE_OPERAND (param, 2),
10000 param_num);
10001 return;
10002 }
b34c7881
JT
10003 }
10004
10005 (*callback) (ctx, param, param_num);
10006}
e2500fed 10007
a98c2819
MLI
10008/* Checks for a builtin function FNDECL that the number of arguments
10009 NARGS against the required number REQUIRED and issues an error if
10010 there is a mismatch. Returns true if the number of arguments is
79ce98bc 10011 correct, otherwise false. LOC is the location of FNDECL. */
83322951
RG
10012
10013static bool
79ce98bc
MP
10014builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
10015 int required)
83322951
RG
10016{
10017 if (nargs < required)
10018 {
e01b4e16 10019 error_at (loc, "too few arguments to function %qE", fndecl);
83322951
RG
10020 return false;
10021 }
10022 else if (nargs > required)
10023 {
79ce98bc 10024 error_at (loc, "too many arguments to function %qE", fndecl);
83322951
RG
10025 return false;
10026 }
10027 return true;
10028}
10029
79ce98bc
MP
10030/* Helper macro for check_builtin_function_arguments. */
10031#define ARG_LOCATION(N) \
10032 (arg_loc.is_empty () \
10033 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
10034 : expansion_point_location (arg_loc[(N)]))
10035
83322951 10036/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
79ce98bc
MP
10037 Returns false if there was an error, otherwise true. LOC is the
10038 location of the function; ARG_LOC is a vector of locations of the
10039 arguments. */
83322951
RG
10040
10041bool
79ce98bc
MP
10042check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
10043 tree fndecl, int nargs, tree *args)
83322951
RG
10044{
10045 if (!DECL_BUILT_IN (fndecl)
10046 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10047 return true;
10048
10049 switch (DECL_FUNCTION_CODE (fndecl))
10050 {
35886f0b
MS
10051 case BUILT_IN_ALLOCA_WITH_ALIGN:
10052 {
10053 /* Get the requested alignment (in bits) if it's a constant
10054 integer expression. */
10055 unsigned HOST_WIDE_INT align
10056 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
10057
10058 /* Determine if the requested alignment is a power of 2. */
10059 if ((align & (align - 1)))
10060 align = 0;
10061
10062 /* The maximum alignment in bits corresponding to the same
10063 maximum in bytes enforced in check_user_alignment(). */
10064 unsigned maxalign = (UINT_MAX >> 1) + 1;
79ce98bc 10065
35886f0b
MS
10066 /* Reject invalid alignments. */
10067 if (align < BITS_PER_UNIT || maxalign < align)
10068 {
79ce98bc 10069 error_at (ARG_LOCATION (1),
35886f0b
MS
10070 "second argument to function %qE must be a constant "
10071 "integer power of 2 between %qi and %qu bits",
10072 fndecl, BITS_PER_UNIT, maxalign);
10073 return false;
10074 }
79ce98bc 10075 return true;
35886f0b
MS
10076 }
10077
83322951 10078 case BUILT_IN_CONSTANT_P:
79ce98bc 10079 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
83322951
RG
10080
10081 case BUILT_IN_ISFINITE:
10082 case BUILT_IN_ISINF:
05f41289 10083 case BUILT_IN_ISINF_SIGN:
83322951
RG
10084 case BUILT_IN_ISNAN:
10085 case BUILT_IN_ISNORMAL:
61717a45 10086 case BUILT_IN_SIGNBIT:
79ce98bc 10087 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
83322951
RG
10088 {
10089 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10090 {
79ce98bc
MP
10091 error_at (ARG_LOCATION (0), "non-floating-point argument in "
10092 "call to function %qE", fndecl);
83322951
RG
10093 return false;
10094 }
10095 return true;
10096 }
10097 return false;
10098
10099 case BUILT_IN_ISGREATER:
10100 case BUILT_IN_ISGREATEREQUAL:
10101 case BUILT_IN_ISLESS:
10102 case BUILT_IN_ISLESSEQUAL:
10103 case BUILT_IN_ISLESSGREATER:
10104 case BUILT_IN_ISUNORDERED:
79ce98bc 10105 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
83322951
RG
10106 {
10107 enum tree_code code0, code1;
10108 code0 = TREE_CODE (TREE_TYPE (args[0]));
10109 code1 = TREE_CODE (TREE_TYPE (args[1]));
10110 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10111 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10112 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10113 {
79ce98bc
MP
10114 error_at (loc, "non-floating-point arguments in call to "
10115 "function %qE", fndecl);
83322951
RG
10116 return false;
10117 }
10118 return true;
10119 }
10120 return false;
10121
3bf5906b 10122 case BUILT_IN_FPCLASSIFY:
79ce98bc 10123 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
3bf5906b 10124 {
79ce98bc 10125 for (unsigned int i = 0; i < 5; i++)
3bf5906b
KG
10126 if (TREE_CODE (args[i]) != INTEGER_CST)
10127 {
79ce98bc
MP
10128 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
10129 "call to function %qE", i + 1, fndecl);
3bf5906b
KG
10130 return false;
10131 }
10132
10133 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10134 {
79ce98bc
MP
10135 error_at (ARG_LOCATION (5), "non-floating-point argument in "
10136 "call to function %qE", fndecl);
3bf5906b
KG
10137 return false;
10138 }
10139 return true;
10140 }
10141 return false;
10142
45d439ac 10143 case BUILT_IN_ASSUME_ALIGNED:
79ce98bc 10144 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
45d439ac
JJ
10145 {
10146 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10147 {
79ce98bc
MP
10148 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
10149 "function %qE", fndecl);
45d439ac
JJ
10150 return false;
10151 }
10152 return true;
10153 }
10154 return false;
10155
1304953e
JJ
10156 case BUILT_IN_ADD_OVERFLOW:
10157 case BUILT_IN_SUB_OVERFLOW:
10158 case BUILT_IN_MUL_OVERFLOW:
79ce98bc 10159 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
1304953e
JJ
10160 {
10161 unsigned i;
10162 for (i = 0; i < 2; i++)
10163 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10164 {
79ce98bc
MP
10165 error_at (ARG_LOCATION (i), "argument %u in call to function "
10166 "%qE does not have integral type", i + 1, fndecl);
1304953e
JJ
10167 return false;
10168 }
10169 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
a86451b9 10170 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
1304953e 10171 {
79ce98bc 10172 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
a86451b9
JJ
10173 "does not have pointer to integral type", fndecl);
10174 return false;
10175 }
10176 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
10177 {
10178 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10179 "has pointer to enumerated type", fndecl);
10180 return false;
10181 }
10182 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
10183 {
10184 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10185 "has pointer to boolean type", fndecl);
1304953e
JJ
10186 return false;
10187 }
10188 return true;
10189 }
10190 return false;
10191
44a845ca
MS
10192 case BUILT_IN_ADD_OVERFLOW_P:
10193 case BUILT_IN_SUB_OVERFLOW_P:
10194 case BUILT_IN_MUL_OVERFLOW_P:
10195 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
10196 {
10197 unsigned i;
10198 for (i = 0; i < 3; i++)
10199 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10200 {
10201 error_at (ARG_LOCATION (i), "argument %u in call to function "
10202 "%qE does not have integral type", i + 1, fndecl);
10203 return false;
10204 }
a86451b9
JJ
10205 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
10206 {
10207 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10208 "%qE has enumerated type", fndecl);
10209 return false;
10210 }
10211 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
10212 {
10213 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10214 "%qE has boolean type", fndecl);
10215 return false;
10216 }
44a845ca
MS
10217 return true;
10218 }
10219 return false;
10220
83322951
RG
10221 default:
10222 return true;
10223 }
10224}
10225
d07605f5
AP
10226/* Function to help qsort sort FIELD_DECLs by name order. */
10227
10228int
10229field_decl_cmp (const void *x_p, const void *y_p)
10230{
28dab132
BI
10231 const tree *const x = (const tree *const) x_p;
10232 const tree *const y = (const tree *const) y_p;
10233
d07605f5
AP
10234 if (DECL_NAME (*x) == DECL_NAME (*y))
10235 /* A nontype is "greater" than a type. */
10236 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10237 if (DECL_NAME (*x) == NULL_TREE)
10238 return -1;
10239 if (DECL_NAME (*y) == NULL_TREE)
10240 return 1;
10241 if (DECL_NAME (*x) < DECL_NAME (*y))
10242 return -1;
10243 return 1;
10244}
10245
10246static struct {
10247 gt_pointer_operator new_value;
10248 void *cookie;
10249} resort_data;
10250
10251/* This routine compares two fields like field_decl_cmp but using the
10252pointer operator in resort_data. */
10253
10254static int
10255resort_field_decl_cmp (const void *x_p, const void *y_p)
10256{
28dab132
BI
10257 const tree *const x = (const tree *const) x_p;
10258 const tree *const y = (const tree *const) y_p;
d07605f5
AP
10259
10260 if (DECL_NAME (*x) == DECL_NAME (*y))
10261 /* A nontype is "greater" than a type. */
10262 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10263 if (DECL_NAME (*x) == NULL_TREE)
10264 return -1;
10265 if (DECL_NAME (*y) == NULL_TREE)
10266 return 1;
10267 {
10268 tree d1 = DECL_NAME (*x);
10269 tree d2 = DECL_NAME (*y);
10270 resort_data.new_value (&d1, resort_data.cookie);
10271 resort_data.new_value (&d2, resort_data.cookie);
10272 if (d1 < d2)
10273 return -1;
10274 }
10275 return 1;
10276}
10277
10278/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10279
10280void
10281resort_sorted_fields (void *obj,
e18476eb 10282 void * ARG_UNUSED (orig_obj),
6de9cd9a
DN
10283 gt_pointer_operator new_value,
10284 void *cookie)
d07605f5 10285{
e18476eb 10286 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
d07605f5
AP
10287 resort_data.new_value = new_value;
10288 resort_data.cookie = cookie;
10289 qsort (&sf->elts[0], sf->len, sizeof (tree),
6de9cd9a 10290 resort_field_decl_cmp);
d07605f5
AP
10291}
10292
0a3ee0fd
GDR
10293/* Subroutine of c_parse_error.
10294 Return the result of concatenating LHS and RHS. RHS is really
10295 a string literal, its first character is indicated by RHS_START and
3292fb42 10296 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
10297
10298 The caller is responsible for deleting the returned pointer. */
10299
10300static char *
10301catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10302{
10303 const int lhs_size = strlen (lhs);
10304 char *result = XNEWVEC (char, lhs_size + rhs_size);
10305 strncpy (result, lhs, lhs_size);
10306 strncpy (result + lhs_size, rhs_start, rhs_size);
10307 return result;
10308}
10309
4b794eaf 10310/* Issue the error given by GMSGID, indicating that it occurred before
4bb8ca28
MM
10311 TOKEN, which had the associated VALUE. */
10312
10313void
b8698a0f 10314c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
cfc93532 10315 tree value, unsigned char token_flags)
4bb8ca28 10316{
0a3ee0fd
GDR
10317#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10318
10319 char *message = NULL;
4bb8ca28 10320
cfc93532 10321 if (token_type == CPP_EOF)
4b794eaf 10322 message = catenate_messages (gmsgid, " at end of input");
b8698a0f
L
10323 else if (token_type == CPP_CHAR
10324 || token_type == CPP_WCHAR
cfc93532 10325 || token_type == CPP_CHAR16
fe95b036
ESR
10326 || token_type == CPP_CHAR32
10327 || token_type == CPP_UTF8CHAR)
4bb8ca28
MM
10328 {
10329 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
10330 const char *prefix;
10331
cfc93532 10332 switch (token_type)
b6baa67d
KVH
10333 {
10334 default:
10335 prefix = "";
10336 break;
10337 case CPP_WCHAR:
10338 prefix = "L";
10339 break;
10340 case CPP_CHAR16:
10341 prefix = "u";
10342 break;
10343 case CPP_CHAR32:
10344 prefix = "U";
10345 break;
fe95b036
ESR
10346 case CPP_UTF8CHAR:
10347 prefix = "u8";
10348 break;
b6baa67d
KVH
10349 }
10350
4bb8ca28 10351 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 10352 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 10353 else
c22cacf3 10354 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 10355
b6baa67d 10356 error (message, prefix, val);
0a3ee0fd
GDR
10357 free (message);
10358 message = NULL;
4bb8ca28 10359 }
65e5a578
ESR
10360 else if (token_type == CPP_CHAR_USERDEF
10361 || token_type == CPP_WCHAR_USERDEF
10362 || token_type == CPP_CHAR16_USERDEF
fe95b036
ESR
10363 || token_type == CPP_CHAR32_USERDEF
10364 || token_type == CPP_UTF8CHAR_USERDEF)
65e5a578
ESR
10365 message = catenate_messages (gmsgid,
10366 " before user-defined character literal");
10367 else if (token_type == CPP_STRING_USERDEF
10368 || token_type == CPP_WSTRING_USERDEF
10369 || token_type == CPP_STRING16_USERDEF
10370 || token_type == CPP_STRING32_USERDEF
10371 || token_type == CPP_UTF8STRING_USERDEF)
10372 message = catenate_messages (gmsgid, " before user-defined string literal");
b8698a0f
L
10373 else if (token_type == CPP_STRING
10374 || token_type == CPP_WSTRING
cfc93532 10375 || token_type == CPP_STRING16
2c6e3f55
JJ
10376 || token_type == CPP_STRING32
10377 || token_type == CPP_UTF8STRING)
4b794eaf 10378 message = catenate_messages (gmsgid, " before string constant");
cfc93532 10379 else if (token_type == CPP_NUMBER)
4b794eaf 10380 message = catenate_messages (gmsgid, " before numeric constant");
cfc93532 10381 else if (token_type == CPP_NAME)
0a3ee0fd 10382 {
4b794eaf 10383 message = catenate_messages (gmsgid, " before %qE");
c51a1ba9 10384 error (message, value);
0a3ee0fd
GDR
10385 free (message);
10386 message = NULL;
10387 }
cfc93532 10388 else if (token_type == CPP_PRAGMA)
bc4071dd 10389 message = catenate_messages (gmsgid, " before %<#pragma%>");
cfc93532 10390 else if (token_type == CPP_PRAGMA_EOL)
bc4071dd 10391 message = catenate_messages (gmsgid, " before end of line");
34429675
JM
10392 else if (token_type == CPP_DECLTYPE)
10393 message = catenate_messages (gmsgid, " before %<decltype%>");
cfc93532 10394 else if (token_type < N_TTYPES)
0a3ee0fd 10395 {
4b794eaf 10396 message = catenate_messages (gmsgid, " before %qs token");
cfc93532 10397 error (message, cpp_type2name (token_type, token_flags));
0a3ee0fd
GDR
10398 free (message);
10399 message = NULL;
10400 }
4bb8ca28 10401 else
4b794eaf 10402 error (gmsgid);
0a3ee0fd
GDR
10403
10404 if (message)
10405 {
10406 error (message);
10407 free (message);
10408 }
c22cacf3 10409#undef catenate_messages
4bb8ca28
MM
10410}
10411
87cf0651
SB
10412/* Return the gcc option code associated with the reason for a cpp
10413 message, or 0 if none. */
10414
10415static int
10416c_option_controlling_cpp_error (int reason)
10417{
b559c810 10418 const struct cpp_reason_option_codes_t *entry;
87cf0651 10419
b559c810 10420 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
87cf0651
SB
10421 {
10422 if (entry->reason == reason)
10423 return entry->option_code;
10424 }
10425 return 0;
10426}
10427
148e4216 10428/* Callback from cpp_error for PFILE to print diagnostics from the
87cf0651
SB
10429 preprocessor. The diagnostic is of type LEVEL, with REASON set
10430 to the reason code if LEVEL is represents a warning, at location
8a645150
DM
10431 RICHLOC unless this is after lexing and the compiler's location
10432 should be used instead; MSG is the translated message and AP
148e4216
JM
10433 the arguments. Returns true if a diagnostic was emitted, false
10434 otherwise. */
10435
10436bool
87cf0651 10437c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8a645150 10438 rich_location *richloc,
148e4216
JM
10439 const char *msg, va_list *ap)
10440{
10441 diagnostic_info diagnostic;
10442 diagnostic_t dlevel;
e3339d0f 10443 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
148e4216
JM
10444 bool ret;
10445
10446 switch (level)
10447 {
10448 case CPP_DL_WARNING_SYSHDR:
10449 if (flag_no_output)
10450 return false;
e3339d0f 10451 global_dc->dc_warn_system_headers = 1;
148e4216
JM
10452 /* Fall through. */
10453 case CPP_DL_WARNING:
10454 if (flag_no_output)
10455 return false;
10456 dlevel = DK_WARNING;
10457 break;
10458 case CPP_DL_PEDWARN:
10459 if (flag_no_output && !flag_pedantic_errors)
10460 return false;
10461 dlevel = DK_PEDWARN;
10462 break;
10463 case CPP_DL_ERROR:
10464 dlevel = DK_ERROR;
10465 break;
10466 case CPP_DL_ICE:
10467 dlevel = DK_ICE;
10468 break;
10469 case CPP_DL_NOTE:
10470 dlevel = DK_NOTE;
10471 break;
47580d22
JM
10472 case CPP_DL_FATAL:
10473 dlevel = DK_FATAL;
10474 break;
148e4216
JM
10475 default:
10476 gcc_unreachable ();
10477 }
10478 if (done_lexing)
f79520bb 10479 richloc->set_range (line_table, 0, input_location, true);
148e4216 10480 diagnostic_set_info_translated (&diagnostic, msg, ap,
8a645150 10481 richloc, dlevel);
87cf0651
SB
10482 diagnostic_override_option_index (&diagnostic,
10483 c_option_controlling_cpp_error (reason));
148e4216
JM
10484 ret = report_diagnostic (&diagnostic);
10485 if (level == CPP_DL_WARNING_SYSHDR)
e3339d0f 10486 global_dc->dc_warn_system_headers = save_warn_system_headers;
148e4216
JM
10487 return ret;
10488}
10489
c5ff069d
ZW
10490/* Convert a character from the host to the target execution character
10491 set. cpplib handles this, mostly. */
10492
10493HOST_WIDE_INT
10494c_common_to_target_charset (HOST_WIDE_INT c)
10495{
10496 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10497 zero-extended under -fno-signed-char. cpplib insists that characters
10498 and character constants are always unsigned. Hence we must convert
10499 back and forth. */
10500 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10501
10502 uc = cpp_host_to_exec_charset (parse_in, uc);
10503
10504 if (flag_signed_char)
10505 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10506 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10507 else
10508 return uc;
10509}
10510
cf9e9959
EB
10511/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10512 references with an INDIRECT_REF of a constant at the bottom; much like the
10513 traditional rendering of offsetof as a macro. Return the folded result. */
ee8a6a3e 10514
cf9e9959 10515tree
c85158de 10516fold_offsetof_1 (tree expr, enum tree_code ctx)
ee8a6a3e 10517{
ee8a6a3e 10518 tree base, off, t;
c85158de
MS
10519 tree_code code = TREE_CODE (expr);
10520 switch (code)
ee8a6a3e
RH
10521 {
10522 case ERROR_MARK:
10523 return expr;
10524
545b7d8c
VR
10525 case VAR_DECL:
10526 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10527 return error_mark_node;
10528
6d4d7b0e 10529 case CALL_EXPR:
8d5f60ac 10530 case TARGET_EXPR:
6d4d7b0e
PB
10531 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10532 return error_mark_node;
10533
6d4d7b0e
PB
10534 case NOP_EXPR:
10535 case INDIRECT_REF:
cf9e9959 10536 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
61c3c490
DS
10537 {
10538 error ("cannot apply %<offsetof%> to a non constant address");
10539 return error_mark_node;
10540 }
cf9e9959 10541 return TREE_OPERAND (expr, 0);
6d4d7b0e 10542
ee8a6a3e 10543 case COMPONENT_REF:
c85158de 10544 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
ee8a6a3e
RH
10545 if (base == error_mark_node)
10546 return base;
10547
10548 t = TREE_OPERAND (expr, 1);
10549 if (DECL_C_BIT_FIELD (t))
10550 {
10551 error ("attempt to take address of bit-field structure "
c51a1ba9 10552 "member %qD", t);
ee8a6a3e
RH
10553 return error_mark_node;
10554 }
db3927fb 10555 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
386b1f1f 10556 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
db3927fb 10557 / BITS_PER_UNIT));
ee8a6a3e
RH
10558 break;
10559
10560 case ARRAY_REF:
c85158de 10561 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
ee8a6a3e
RH
10562 if (base == error_mark_node)
10563 return base;
10564
10565 t = TREE_OPERAND (expr, 1);
61c3c490
DS
10566
10567 /* Check if the offset goes beyond the upper bound of the array. */
cf9e9959 10568 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
d32599a6
JJ
10569 {
10570 tree upbound = array_ref_up_bound (expr);
10571 if (upbound != NULL_TREE
10572 && TREE_CODE (upbound) == INTEGER_CST
10573 && !tree_int_cst_equal (upbound,
10574 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10575 {
c85158de
MS
10576 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10577 upbound = size_binop (PLUS_EXPR, upbound,
10578 build_int_cst (TREE_TYPE (upbound), 1));
d32599a6
JJ
10579 if (tree_int_cst_lt (upbound, t))
10580 {
10581 tree v;
10582
10583 for (v = TREE_OPERAND (expr, 0);
10584 TREE_CODE (v) == COMPONENT_REF;
10585 v = TREE_OPERAND (v, 0))
10586 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10587 == RECORD_TYPE)
10588 {
910ad8de
NF
10589 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10590 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
d32599a6
JJ
10591 if (TREE_CODE (fld_chain) == FIELD_DECL)
10592 break;
10593
10594 if (fld_chain)
10595 break;
10596 }
10597 /* Don't warn if the array might be considered a poor
10598 man's flexible array member with a very permissive
10599 definition thereof. */
10600 if (TREE_CODE (v) == ARRAY_REF
10601 || TREE_CODE (v) == COMPONENT_REF)
10602 warning (OPT_Warray_bounds,
10603 "index %E denotes an offset "
10604 "greater than size of %qT",
10605 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10606 }
10607 }
10608 }
cf9e9959
EB
10609
10610 t = convert (sizetype, t);
10611 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
ee8a6a3e
RH
10612 break;
10613
1916c916
VR
10614 case COMPOUND_EXPR:
10615 /* Handle static members of volatile structs. */
10616 t = TREE_OPERAND (expr, 1);
0ae9bd27 10617 gcc_assert (VAR_P (t));
cf9e9959 10618 return fold_offsetof_1 (t);
1916c916 10619
ee8a6a3e 10620 default:
366de0ce 10621 gcc_unreachable ();
ee8a6a3e
RH
10622 }
10623
cf9e9959 10624 return fold_build_pointer_plus (base, off);
ee8a6a3e
RH
10625}
10626
cf9e9959
EB
10627/* Likewise, but convert it to the return type of offsetof. */
10628
ee8a6a3e 10629tree
cf9e9959 10630fold_offsetof (tree expr)
ee8a6a3e 10631{
cf9e9959 10632 return convert (size_type_node, fold_offsetof_1 (expr));
ee8a6a3e
RH
10633}
10634
54dcdb88 10635/* Warn for A ?: C expressions (with B omitted) where A is a boolean
d166d4c3
AK
10636 expression, because B will always be true. */
10637
10638void
54dcdb88
BE
10639warn_for_omitted_condop (location_t location, tree cond)
10640{
10641 /* In C++ template declarations it can happen that the type is dependent
10642 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
10643 if (truth_value_p (TREE_CODE (cond))
10644 || (TREE_TYPE (cond) != NULL_TREE
10645 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE))
10646 warning_at (location, OPT_Wparentheses,
d166d4c3
AK
10647 "the omitted middle operand in ?: will always be %<true%>, "
10648 "suggest explicit middle operand");
54dcdb88 10649}
d166d4c3 10650
4816c593
NF
10651/* Give an error for storing into ARG, which is 'const'. USE indicates
10652 how ARG was being used. */
10653
10654void
c02065fc 10655readonly_error (location_t loc, tree arg, enum lvalue_use use)
4816c593
NF
10656{
10657 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10658 || use == lv_asm);
10659 /* Using this macro rather than (for example) arrays of messages
10660 ensures that all the format strings are checked at compile
10661 time. */
10662#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10663 : (use == lv_increment ? (I) \
10664 : (use == lv_decrement ? (D) : (AS))))
10665 if (TREE_CODE (arg) == COMPONENT_REF)
10666 {
10667 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
c02065fc
AH
10668 error_at (loc, READONLY_MSG (G_("assignment of member "
10669 "%qD in read-only object"),
10670 G_("increment of member "
10671 "%qD in read-only object"),
10672 G_("decrement of member "
10673 "%qD in read-only object"),
10674 G_("member %qD in read-only object "
10675 "used as %<asm%> output")),
10676 TREE_OPERAND (arg, 1));
4816c593 10677 else
c02065fc
AH
10678 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10679 G_("increment of read-only member %qD"),
10680 G_("decrement of read-only member %qD"),
10681 G_("read-only member %qD used as %<asm%> output")),
10682 TREE_OPERAND (arg, 1));
4816c593 10683 }
0ae9bd27 10684 else if (VAR_P (arg))
c02065fc
AH
10685 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10686 G_("increment of read-only variable %qD"),
10687 G_("decrement of read-only variable %qD"),
10688 G_("read-only variable %qD used as %<asm%> output")),
10689 arg);
4816c593 10690 else if (TREE_CODE (arg) == PARM_DECL)
c02065fc
AH
10691 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10692 G_("increment of read-only parameter %qD"),
10693 G_("decrement of read-only parameter %qD"),
10694 G_("read-only parameter %qD use as %<asm%> output")),
10695 arg);
4816c593
NF
10696 else if (TREE_CODE (arg) == RESULT_DECL)
10697 {
10698 gcc_assert (c_dialect_cxx ());
c02065fc
AH
10699 error_at (loc, READONLY_MSG (G_("assignment of "
10700 "read-only named return value %qD"),
10701 G_("increment of "
10702 "read-only named return value %qD"),
10703 G_("decrement of "
10704 "read-only named return value %qD"),
10705 G_("read-only named return value %qD "
10706 "used as %<asm%>output")),
10707 arg);
4816c593
NF
10708 }
10709 else if (TREE_CODE (arg) == FUNCTION_DECL)
c02065fc
AH
10710 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10711 G_("increment of function %qD"),
10712 G_("decrement of function %qD"),
10713 G_("function %qD used as %<asm%> output")),
10714 arg);
4816c593 10715 else
c02065fc
AH
10716 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10717 G_("increment of read-only location %qE"),
10718 G_("decrement of read-only location %qE"),
10719 G_("read-only location %qE used as %<asm%> output")),
10720 arg);
4816c593
NF
10721}
10722
37dc0d8d 10723/* Print an error message for an invalid lvalue. USE says
7bd11157
TT
10724 how the lvalue is being used and so selects the error message. LOC
10725 is the location for the error. */
5ae9ba3e 10726
37dc0d8d 10727void
7bd11157 10728lvalue_error (location_t loc, enum lvalue_use use)
5ae9ba3e 10729{
37dc0d8d 10730 switch (use)
5ae9ba3e 10731 {
37dc0d8d 10732 case lv_assign:
7bd11157 10733 error_at (loc, "lvalue required as left operand of assignment");
37dc0d8d
JM
10734 break;
10735 case lv_increment:
7bd11157 10736 error_at (loc, "lvalue required as increment operand");
37dc0d8d
JM
10737 break;
10738 case lv_decrement:
7bd11157 10739 error_at (loc, "lvalue required as decrement operand");
37dc0d8d
JM
10740 break;
10741 case lv_addressof:
7bd11157 10742 error_at (loc, "lvalue required as unary %<&%> operand");
37dc0d8d
JM
10743 break;
10744 case lv_asm:
7bd11157 10745 error_at (loc, "lvalue required in asm statement");
37dc0d8d
JM
10746 break;
10747 default:
10748 gcc_unreachable ();
5ae9ba3e 10749 }
5ae9ba3e 10750}
7a6daeb0
NF
10751
10752/* Print an error message for an invalid indirection of type TYPE.
10753 ERRSTRING is the name of the operator for the indirection. */
10754
10755void
10756invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10757{
10758 switch (errstring)
10759 {
10760 case RO_NULL:
10761 gcc_assert (c_dialect_cxx ());
10762 error_at (loc, "invalid type argument (have %qT)", type);
10763 break;
10764 case RO_ARRAY_INDEXING:
10765 error_at (loc,
10766 "invalid type argument of array indexing (have %qT)",
10767 type);
10768 break;
10769 case RO_UNARY_STAR:
10770 error_at (loc,
10771 "invalid type argument of unary %<*%> (have %qT)",
10772 type);
10773 break;
10774 case RO_ARROW:
10775 error_at (loc,
10776 "invalid type argument of %<->%> (have %qT)",
10777 type);
10778 break;
55a7f02f
MP
10779 case RO_ARROW_STAR:
10780 error_at (loc,
10781 "invalid type argument of %<->*%> (have %qT)",
10782 type);
10783 break;
7a6daeb0
NF
10784 case RO_IMPLICIT_CONVERSION:
10785 error_at (loc,
10786 "invalid type argument of implicit conversion (have %qT)",
10787 type);
10788 break;
10789 default:
10790 gcc_unreachable ();
10791 }
10792}
aab038d5
RH
10793\f
10794/* *PTYPE is an incomplete array. Complete it with a domain based on
10795 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10796 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10797 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10798
10799int
10800complete_array_type (tree *ptype, tree initial_value, bool do_default)
10801{
10802 tree maxindex, type, main_type, elt, unqual_elt;
10803 int failure = 0, quals;
06d40de8 10804 hashval_t hashcode = 0;
40d3d530 10805 bool overflow_p = false;
aab038d5
RH
10806
10807 maxindex = size_zero_node;
10808 if (initial_value)
10809 {
10810 if (TREE_CODE (initial_value) == STRING_CST)
10811 {
10812 int eltsize
10813 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10814 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10815 }
10816 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10817 {
9771b263 10818 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 10819
9771b263 10820 if (vec_safe_is_empty (v))
aab038d5
RH
10821 {
10822 if (pedantic)
10823 failure = 3;
830c740f 10824 maxindex = ssize_int (-1);
aab038d5
RH
10825 }
10826 else
10827 {
10828 tree curindex;
4038c495
GB
10829 unsigned HOST_WIDE_INT cnt;
10830 constructor_elt *ce;
cff7525f 10831 bool fold_p = false;
aab038d5 10832
9771b263 10833 if ((*v)[0].index)
40d3d530
JR
10834 maxindex = (*v)[0].index, fold_p = true;
10835
aab038d5
RH
10836 curindex = maxindex;
10837
9771b263 10838 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
aab038d5 10839 {
cff7525f 10840 bool curfold_p = false;
4038c495 10841 if (ce->index)
cff7525f 10842 curindex = ce->index, curfold_p = true;
aab038d5 10843 else
cff7525f
JH
10844 {
10845 if (fold_p)
40d3d530
JR
10846 {
10847 /* Since we treat size types now as ordinary
10848 unsigned types, we need an explicit overflow
10849 check. */
10850 tree orig = curindex;
10851 curindex = fold_convert (sizetype, curindex);
10852 overflow_p |= tree_int_cst_lt (curindex, orig);
10853 }
db3927fb
AH
10854 curindex = size_binop (PLUS_EXPR, curindex,
10855 size_one_node);
cff7525f 10856 }
aab038d5 10857 if (tree_int_cst_lt (maxindex, curindex))
cff7525f 10858 maxindex = curindex, fold_p = curfold_p;
aab038d5 10859 }
40d3d530
JR
10860 if (fold_p)
10861 {
10862 tree orig = maxindex;
10863 maxindex = fold_convert (sizetype, maxindex);
10864 overflow_p |= tree_int_cst_lt (maxindex, orig);
10865 }
aab038d5
RH
10866 }
10867 }
10868 else
10869 {
10870 /* Make an error message unless that happened already. */
10871 if (initial_value != error_mark_node)
10872 failure = 1;
10873 }
10874 }
10875 else
10876 {
10877 failure = 2;
10878 if (!do_default)
10879 return failure;
10880 }
10881
10882 type = *ptype;
10883 elt = TREE_TYPE (type);
10884 quals = TYPE_QUALS (strip_array_types (elt));
10885 if (quals == 0)
10886 unqual_elt = elt;
10887 else
36c5e70a 10888 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
aab038d5
RH
10889
10890 /* Using build_distinct_type_copy and modifying things afterward instead
10891 of using build_array_type to create a new type preserves all of the
10892 TYPE_LANG_FLAG_? bits that the front end may have set. */
10893 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10894 TREE_TYPE (main_type) = unqual_elt;
e6313a78
RG
10895 TYPE_DOMAIN (main_type)
10896 = build_range_type (TREE_TYPE (maxindex),
10897 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
aab038d5
RH
10898 layout_type (main_type);
10899
06d40de8
DG
10900 /* Make sure we have the canonical MAIN_TYPE. */
10901 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
b8698a0f 10902 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
06d40de8
DG
10903 hashcode);
10904 main_type = type_hash_canon (hashcode, main_type);
10905
9ae165a0
DG
10906 /* Fix the canonical type. */
10907 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10908 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10909 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10910 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10911 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10912 != TYPE_DOMAIN (main_type)))
b8698a0f 10913 TYPE_CANONICAL (main_type)
9ae165a0
DG
10914 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10915 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10916 else
10917 TYPE_CANONICAL (main_type) = main_type;
10918
aab038d5
RH
10919 if (quals == 0)
10920 type = main_type;
10921 else
10922 type = c_build_qualified_type (main_type, quals);
10923
7bfcb402
JM
10924 if (COMPLETE_TYPE_P (type)
10925 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
40d3d530 10926 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7bfcb402
JM
10927 {
10928 error ("size of array is too large");
10929 /* If we proceed with the array type as it is, we'll eventually
386b1f1f 10930 crash in tree_to_[su]hwi(). */
7bfcb402
JM
10931 type = error_mark_node;
10932 }
10933
aab038d5
RH
10934 *ptype = type;
10935 return failure;
10936}
5ae9ba3e 10937
30cd1c5d
AS
10938/* Like c_mark_addressable but don't check register qualifier. */
10939void
10940c_common_mark_addressable_vec (tree t)
10941{
10942 while (handled_component_p (t))
10943 t = TREE_OPERAND (t, 0);
b772a565
RB
10944 if (!VAR_P (t)
10945 && TREE_CODE (t) != PARM_DECL
10946 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
30cd1c5d
AS
10947 return;
10948 TREE_ADDRESSABLE (t) = 1;
10949}
10950
10951
48ae6c13
RH
10952\f
10953/* Used to help initialize the builtin-types.def table. When a type of
10954 the correct size doesn't exist, use error_mark_node instead of NULL.
10955 The later results in segfaults even when a decl using the type doesn't
10956 get invoked. */
10957
10958tree
10959builtin_type_for_size (int size, bool unsignedp)
10960{
21fa2faf 10961 tree type = c_common_type_for_size (size, unsignedp);
48ae6c13
RH
10962 return type ? type : error_mark_node;
10963}
10964
10965/* A helper function for resolve_overloaded_builtin in resolving the
10966 overloaded __sync_ builtins. Returns a positive power of 2 if the
10967 first operand of PARAMS is a pointer to a supported data type.
7a127fa7
MS
10968 Returns 0 if an error is encountered.
10969 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10970 built-ins. */
48ae6c13
RH
10971
10972static int
7a127fa7 10973sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
48ae6c13 10974{
7a127fa7
MS
10975 /* Type of the argument. */
10976 tree argtype;
10977 /* Type the argument points to. */
48ae6c13
RH
10978 tree type;
10979 int size;
10980
ba6b3795 10981 if (vec_safe_is_empty (params))
48ae6c13
RH
10982 {
10983 error ("too few arguments to function %qE", function);
10984 return 0;
10985 }
10986
7a127fa7 10987 argtype = type = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10988 if (TREE_CODE (type) == ARRAY_TYPE)
10989 {
10990 /* Force array-to-pointer decay for C++. */
10991 gcc_assert (c_dialect_cxx());
10992 (*params)[0] = default_conversion ((*params)[0]);
10993 type = TREE_TYPE ((*params)[0]);
10994 }
48ae6c13
RH
10995 if (TREE_CODE (type) != POINTER_TYPE)
10996 goto incompatible;
10997
10998 type = TREE_TYPE (type);
10999 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11000 goto incompatible;
11001
7a127fa7
MS
11002 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
11003 goto incompatible;
11004
ae7e9ddd 11005 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
a0274e3e 11006 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
11007 return size;
11008
11009 incompatible:
9f04a53e
MS
11010 /* Issue the diagnostic only if the argument is valid, otherwise
11011 it would be redundant at best and could be misleading. */
11012 if (argtype != error_mark_node)
11013 error ("operand type %qT is incompatible with argument %d of %qE",
11014 argtype, 1, function);
48ae6c13
RH
11015 return 0;
11016}
11017
c22cacf3 11018/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
11019 PARAMS to make arguments match up with those of FUNCTION. Drops
11020 the variadic arguments at the end. Returns false if some error
11021 was encountered; true on success. */
11022
11023static bool
86951993 11024sync_resolve_params (location_t loc, tree orig_function, tree function,
9771b263 11025 vec<tree, va_gc> *params, bool orig_format)
48ae6c13 11026{
e19a18d4 11027 function_args_iterator iter;
48ae6c13 11028 tree ptype;
bbbbb16a 11029 unsigned int parmnum;
48ae6c13 11030
e19a18d4 11031 function_args_iter_init (&iter, TREE_TYPE (function));
48ae6c13
RH
11032 /* We've declared the implementation functions to use "volatile void *"
11033 as the pointer parameter, so we shouldn't get any complaints from the
11034 call to check_function_arguments what ever type the user used. */
e19a18d4 11035 function_args_iter_next (&iter);
9771b263 11036 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
267bac10 11037 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
11038
11039 /* For the rest of the values, we need to cast these to FTYPE, so that we
11040 don't get warnings for passing pointer types, etc. */
bbbbb16a 11041 parmnum = 0;
e19a18d4 11042 while (1)
48ae6c13 11043 {
e19a18d4
NF
11044 tree val, arg_type;
11045
11046 arg_type = function_args_iter_cond (&iter);
11047 /* XXX void_type_node belies the abstraction. */
11048 if (arg_type == void_type_node)
11049 break;
48ae6c13 11050
bbbbb16a 11051 ++parmnum;
9771b263 11052 if (params->length () <= parmnum)
48ae6c13 11053 {
86951993 11054 error_at (loc, "too few arguments to function %qE", orig_function);
48ae6c13
RH
11055 return false;
11056 }
11057
e3793c6f
JJ
11058 /* Only convert parameters if arg_type is unsigned integer type with
11059 new format sync routines, i.e. don't attempt to convert pointer
11060 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11061 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11062 kinds). */
11063 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
86951993
AM
11064 {
11065 /* Ideally for the first conversion we'd use convert_for_assignment
11066 so that we get warnings for anything that doesn't match the pointer
11067 type. This isn't portable across the C and C++ front ends atm. */
9771b263 11068 val = (*params)[parmnum];
86951993
AM
11069 val = convert (ptype, val);
11070 val = convert (arg_type, val);
9771b263 11071 (*params)[parmnum] = val;
86951993 11072 }
48ae6c13 11073
e19a18d4 11074 function_args_iter_next (&iter);
48ae6c13
RH
11075 }
11076
86951993 11077 /* __atomic routines are not variadic. */
9771b263 11078 if (!orig_format && params->length () != parmnum + 1)
86951993
AM
11079 {
11080 error_at (loc, "too many arguments to function %qE", orig_function);
11081 return false;
11082 }
11083
48ae6c13
RH
11084 /* The definition of these primitives is variadic, with the remaining
11085 being "an optional list of variables protected by the memory barrier".
11086 No clue what that's supposed to mean, precisely, but we consider all
11087 call-clobbered variables to be protected so we're safe. */
9771b263 11088 params->truncate (parmnum + 1);
48ae6c13
RH
11089
11090 return true;
11091}
11092
c22cacf3 11093/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
11094 RESULT to make it match the type of the first pointer argument in
11095 PARAMS. */
11096
11097static tree
86951993 11098sync_resolve_return (tree first_param, tree result, bool orig_format)
48ae6c13 11099{
bbbbb16a 11100 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
86951993 11101 tree rtype = TREE_TYPE (result);
99db1ef0 11102 ptype = TYPE_MAIN_VARIANT (ptype);
86951993
AM
11103
11104 /* New format doesn't require casting unless the types are the same size. */
11105 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11106 return convert (ptype, result);
11107 else
11108 return result;
11109}
11110
11111/* This function verifies the PARAMS to generic atomic FUNCTION.
11112 It returns the size if all the parameters are the same size, otherwise
11113 0 is returned if the parameters are invalid. */
11114
11115static int
9771b263
DN
11116get_atomic_generic_size (location_t loc, tree function,
11117 vec<tree, va_gc> *params)
86951993
AM
11118{
11119 unsigned int n_param;
11120 unsigned int n_model;
11121 unsigned int x;
11122 int size_0;
11123 tree type_0;
11124
11125 /* Determine the parameter makeup. */
11126 switch (DECL_FUNCTION_CODE (function))
11127 {
11128 case BUILT_IN_ATOMIC_EXCHANGE:
11129 n_param = 4;
11130 n_model = 1;
11131 break;
11132 case BUILT_IN_ATOMIC_LOAD:
11133 case BUILT_IN_ATOMIC_STORE:
11134 n_param = 3;
11135 n_model = 1;
11136 break;
11137 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11138 n_param = 6;
11139 n_model = 2;
11140 break;
11141 default:
c466c4ff 11142 gcc_unreachable ();
86951993
AM
11143 }
11144
9771b263 11145 if (vec_safe_length (params) != n_param)
86951993
AM
11146 {
11147 error_at (loc, "incorrect number of arguments to function %qE", function);
11148 return 0;
11149 }
11150
11151 /* Get type of first parameter, and determine its size. */
9771b263 11152 type_0 = TREE_TYPE ((*params)[0]);
6415bd5d
JM
11153 if (TREE_CODE (type_0) == ARRAY_TYPE)
11154 {
11155 /* Force array-to-pointer decay for C++. */
11156 gcc_assert (c_dialect_cxx());
11157 (*params)[0] = default_conversion ((*params)[0]);
11158 type_0 = TREE_TYPE ((*params)[0]);
11159 }
c466c4ff
AM
11160 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11161 {
11162 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11163 function);
11164 return 0;
11165 }
11166
11167 /* Types must be compile time constant sizes. */
11168 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
86951993 11169 {
c466c4ff
AM
11170 error_at (loc,
11171 "argument 1 of %qE must be a pointer to a constant size type",
11172 function);
86951993
AM
11173 return 0;
11174 }
c466c4ff 11175
ae7e9ddd 11176 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
86951993 11177
c466c4ff
AM
11178 /* Zero size objects are not allowed. */
11179 if (size_0 == 0)
11180 {
11181 error_at (loc,
11182 "argument 1 of %qE must be a pointer to a nonzero size object",
11183 function);
11184 return 0;
11185 }
11186
86951993
AM
11187 /* Check each other parameter is a pointer and the same size. */
11188 for (x = 0; x < n_param - n_model; x++)
11189 {
11190 int size;
9771b263 11191 tree type = TREE_TYPE ((*params)[x]);
688010ba 11192 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
86951993
AM
11193 if (n_param == 6 && x == 3)
11194 continue;
11195 if (!POINTER_TYPE_P (type))
11196 {
11197 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11198 function);
11199 return 0;
11200 }
a76989dc
MP
11201 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
11202 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
11203 != INTEGER_CST)
11204 {
11205 error_at (loc, "argument %d of %qE must be a pointer to a constant "
11206 "size type", x + 1, function);
11207 return 0;
11208 }
11209 else if (FUNCTION_POINTER_TYPE_P (type))
11210 {
11211 error_at (loc, "argument %d of %qE must not be a pointer to a "
11212 "function", x + 1, function);
11213 return 0;
11214 }
7b56b2f8
MP
11215 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11216 size = type_size ? tree_to_uhwi (type_size) : 0;
86951993
AM
11217 if (size != size_0)
11218 {
11219 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11220 function);
11221 return 0;
11222 }
11223 }
11224
11225 /* Check memory model parameters for validity. */
11226 for (x = n_param - n_model ; x < n_param; x++)
11227 {
9771b263 11228 tree p = (*params)[x];
86951993
AM
11229 if (TREE_CODE (p) == INTEGER_CST)
11230 {
ae7e9ddd 11231 int i = tree_to_uhwi (p);
46b35980 11232 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
86951993
AM
11233 {
11234 warning_at (loc, OPT_Winvalid_memory_model,
11235 "invalid memory model argument %d of %qE", x + 1,
11236 function);
86951993
AM
11237 }
11238 }
11239 else
11240 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11241 {
11242 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11243 function);
11244 return 0;
11245 }
11246 }
11247
11248 return size_0;
11249}
11250
11251
11252/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11253 at the beginning of the parameter list PARAMS representing the size of the
11254 objects. This is to match the library ABI requirement. LOC is the location
11255 of the function call.
11256 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11257 returned to allow the external call to be constructed. */
11258
11259static tree
11260add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9771b263 11261 vec<tree, va_gc> *params)
86951993
AM
11262{
11263 tree size_node;
11264
11265 /* Insert a SIZE_T parameter as the first param. If there isn't
11266 enough space, allocate a new vector and recursively re-build with that. */
9771b263 11267 if (!params->space (1))
86951993
AM
11268 {
11269 unsigned int z, len;
9771b263 11270 vec<tree, va_gc> *v;
86951993
AM
11271 tree f;
11272
9771b263
DN
11273 len = params->length ();
11274 vec_alloc (v, len + 1);
8edbfaa6 11275 v->quick_push (build_int_cst (size_type_node, n));
86951993 11276 for (z = 0; z < len; z++)
9771b263 11277 v->quick_push ((*params)[z]);
81e5eca8 11278 f = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 11279 vec_free (v);
86951993
AM
11280 return f;
11281 }
11282
11283 /* Add the size parameter and leave as a function call for processing. */
11284 size_node = build_int_cst (size_type_node, n);
9771b263 11285 params->quick_insert (0, size_node);
86951993
AM
11286 return NULL_TREE;
11287}
11288
11289
6b28e197
JM
11290/* Return whether atomic operations for naturally aligned N-byte
11291 arguments are supported, whether inline or through libatomic. */
11292static bool
11293atomic_size_supported_p (int n)
11294{
11295 switch (n)
11296 {
11297 case 1:
11298 case 2:
11299 case 4:
11300 case 8:
11301 return true;
11302
11303 case 16:
11304 return targetm.scalar_mode_supported_p (TImode);
11305
11306 default:
11307 return false;
11308 }
11309}
11310
86951993
AM
11311/* This will process an __atomic_exchange function call, determine whether it
11312 needs to be mapped to the _N variation, or turned into a library call.
11313 LOC is the location of the builtin call.
11314 FUNCTION is the DECL that has been invoked;
11315 PARAMS is the argument list for the call. The return value is non-null
11316 TRUE is returned if it is translated into the proper format for a call to the
11317 external library, and NEW_RETURN is set the tree for that function.
11318 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11319 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11320static bool
11321resolve_overloaded_atomic_exchange (location_t loc, tree function,
9771b263 11322 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11323{
11324 tree p0, p1, p2, p3;
11325 tree I_type, I_type_ptr;
11326 int n = get_atomic_generic_size (loc, function, params);
11327
c466c4ff
AM
11328 /* Size of 0 is an error condition. */
11329 if (n == 0)
11330 {
11331 *new_return = error_mark_node;
11332 return true;
11333 }
11334
86951993 11335 /* If not a lock-free size, change to the library generic format. */
6b28e197 11336 if (!atomic_size_supported_p (n))
86951993
AM
11337 {
11338 *new_return = add_atomic_size_parameter (n, loc, function, params);
11339 return true;
11340 }
11341
11342 /* Otherwise there is a lockfree match, transform the call from:
11343 void fn(T* mem, T* desired, T* return, model)
11344 into
11345 *return = (T) (fn (In* mem, (In) *desired, model)) */
11346
9771b263
DN
11347 p0 = (*params)[0];
11348 p1 = (*params)[1];
11349 p2 = (*params)[2];
11350 p3 = (*params)[3];
86951993
AM
11351
11352 /* Create pointer to appropriate size. */
11353 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11354 I_type_ptr = build_pointer_type (I_type);
11355
11356 /* Convert object pointer to required type. */
11357 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11358 (*params)[0] = p0;
86951993
AM
11359 /* Convert new value to required type, and dereference it. */
11360 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11361 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11362 (*params)[1] = p1;
86951993
AM
11363
11364 /* Move memory model to the 3rd position, and end param list. */
9771b263
DN
11365 (*params)[2] = p3;
11366 params->truncate (3);
86951993
AM
11367
11368 /* Convert return pointer and dereference it for later assignment. */
11369 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11370
11371 return false;
48ae6c13
RH
11372}
11373
86951993
AM
11374
11375/* This will process an __atomic_compare_exchange function call, determine
11376 whether it needs to be mapped to the _N variation, or turned into a lib call.
11377 LOC is the location of the builtin call.
11378 FUNCTION is the DECL that has been invoked;
11379 PARAMS is the argument list for the call. The return value is non-null
11380 TRUE is returned if it is translated into the proper format for a call to the
11381 external library, and NEW_RETURN is set the tree for that function.
11382 FALSE is returned if processing for the _N variation is required. */
11383
11384static bool
11385resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9771b263 11386 vec<tree, va_gc> *params,
86951993
AM
11387 tree *new_return)
11388{
11389 tree p0, p1, p2;
11390 tree I_type, I_type_ptr;
11391 int n = get_atomic_generic_size (loc, function, params);
11392
c466c4ff
AM
11393 /* Size of 0 is an error condition. */
11394 if (n == 0)
11395 {
11396 *new_return = error_mark_node;
11397 return true;
11398 }
11399
86951993 11400 /* If not a lock-free size, change to the library generic format. */
6b28e197 11401 if (!atomic_size_supported_p (n))
86951993
AM
11402 {
11403 /* The library generic format does not have the weak parameter, so
11404 remove it from the param list. Since a parameter has been removed,
11405 we can be sure that there is room for the SIZE_T parameter, meaning
11406 there will not be a recursive rebuilding of the parameter list, so
11407 there is no danger this will be done twice. */
11408 if (n > 0)
11409 {
9771b263
DN
11410 (*params)[3] = (*params)[4];
11411 (*params)[4] = (*params)[5];
11412 params->truncate (5);
86951993
AM
11413 }
11414 *new_return = add_atomic_size_parameter (n, loc, function, params);
11415 return true;
11416 }
11417
11418 /* Otherwise, there is a match, so the call needs to be transformed from:
11419 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11420 into
11421 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11422
9771b263
DN
11423 p0 = (*params)[0];
11424 p1 = (*params)[1];
11425 p2 = (*params)[2];
86951993
AM
11426
11427 /* Create pointer to appropriate size. */
11428 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11429 I_type_ptr = build_pointer_type (I_type);
11430
11431 /* Convert object pointer to required type. */
11432 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11433 (*params)[0] = p0;
86951993
AM
11434
11435 /* Convert expected pointer to required type. */
11436 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9771b263 11437 (*params)[1] = p1;
86951993
AM
11438
11439 /* Convert desired value to required type, and dereference it. */
11440 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11441 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9771b263 11442 (*params)[2] = p2;
86951993
AM
11443
11444 /* The rest of the parameters are fine. NULL means no special return value
11445 processing.*/
11446 *new_return = NULL;
11447 return false;
11448}
11449
11450
11451/* This will process an __atomic_load function call, determine whether it
11452 needs to be mapped to the _N variation, or turned into a library call.
11453 LOC is the location of the builtin call.
11454 FUNCTION is the DECL that has been invoked;
11455 PARAMS is the argument list for the call. The return value is non-null
11456 TRUE is returned if it is translated into the proper format for a call to the
11457 external library, and NEW_RETURN is set the tree for that function.
11458 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11459 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11460
11461static bool
11462resolve_overloaded_atomic_load (location_t loc, tree function,
9771b263 11463 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11464{
11465 tree p0, p1, p2;
11466 tree I_type, I_type_ptr;
11467 int n = get_atomic_generic_size (loc, function, params);
11468
c466c4ff
AM
11469 /* Size of 0 is an error condition. */
11470 if (n == 0)
11471 {
11472 *new_return = error_mark_node;
11473 return true;
11474 }
11475
86951993 11476 /* If not a lock-free size, change to the library generic format. */
6b28e197 11477 if (!atomic_size_supported_p (n))
86951993
AM
11478 {
11479 *new_return = add_atomic_size_parameter (n, loc, function, params);
11480 return true;
11481 }
11482
11483 /* Otherwise, there is a match, so the call needs to be transformed from:
11484 void fn(T* mem, T* return, model)
11485 into
11486 *return = (T) (fn ((In *) mem, model)) */
11487
9771b263
DN
11488 p0 = (*params)[0];
11489 p1 = (*params)[1];
11490 p2 = (*params)[2];
86951993
AM
11491
11492 /* Create pointer to appropriate size. */
11493 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11494 I_type_ptr = build_pointer_type (I_type);
11495
11496 /* Convert object pointer to required type. */
11497 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11498 (*params)[0] = p0;
86951993
AM
11499
11500 /* Move memory model to the 2nd position, and end param list. */
9771b263
DN
11501 (*params)[1] = p2;
11502 params->truncate (2);
86951993
AM
11503
11504 /* Convert return pointer and dereference it for later assignment. */
11505 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11506
11507 return false;
11508}
11509
11510
11511/* This will process an __atomic_store function call, determine whether it
11512 needs to be mapped to the _N variation, or turned into a library call.
11513 LOC is the location of the builtin call.
11514 FUNCTION is the DECL that has been invoked;
11515 PARAMS is the argument list for the call. The return value is non-null
11516 TRUE is returned if it is translated into the proper format for a call to the
11517 external library, and NEW_RETURN is set the tree for that function.
11518 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11519 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11520
11521static bool
11522resolve_overloaded_atomic_store (location_t loc, tree function,
9771b263 11523 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11524{
11525 tree p0, p1;
11526 tree I_type, I_type_ptr;
11527 int n = get_atomic_generic_size (loc, function, params);
11528
c466c4ff
AM
11529 /* Size of 0 is an error condition. */
11530 if (n == 0)
11531 {
11532 *new_return = error_mark_node;
11533 return true;
11534 }
11535
86951993 11536 /* If not a lock-free size, change to the library generic format. */
6b28e197 11537 if (!atomic_size_supported_p (n))
86951993
AM
11538 {
11539 *new_return = add_atomic_size_parameter (n, loc, function, params);
11540 return true;
11541 }
11542
11543 /* Otherwise, there is a match, so the call needs to be transformed from:
11544 void fn(T* mem, T* value, model)
11545 into
11546 fn ((In *) mem, (In) *value, model) */
11547
9771b263
DN
11548 p0 = (*params)[0];
11549 p1 = (*params)[1];
86951993
AM
11550
11551 /* Create pointer to appropriate size. */
11552 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11553 I_type_ptr = build_pointer_type (I_type);
11554
11555 /* Convert object pointer to required type. */
11556 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11557 (*params)[0] = p0;
86951993
AM
11558
11559 /* Convert new value to required type, and dereference it. */
11560 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11561 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11562 (*params)[1] = p1;
86951993
AM
11563
11564 /* The memory model is in the right spot already. Return is void. */
11565 *new_return = NULL_TREE;
11566
11567 return false;
11568}
11569
11570
48ae6c13
RH
11571/* Some builtin functions are placeholders for other expressions. This
11572 function should be called immediately after parsing the call expression
11573 before surrounding code has committed to the type of the expression.
11574
c2255bc4
AH
11575 LOC is the location of the builtin call.
11576
48ae6c13
RH
11577 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11578 PARAMS is the argument list for the call. The return value is non-null
11579 when expansion is complete, and null if normal processing should
11580 continue. */
11581
11582tree
9771b263
DN
11583resolve_overloaded_builtin (location_t loc, tree function,
11584 vec<tree, va_gc> *params)
48ae6c13
RH
11585{
11586 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7a127fa7
MS
11587
11588 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11589 Those are not valid to call with a pointer to _Bool (or C++ bool)
11590 and so must be rejected. */
11591 bool fetch_op = true;
86951993
AM
11592 bool orig_format = true;
11593 tree new_return = NULL_TREE;
11594
58646b77
PB
11595 switch (DECL_BUILT_IN_CLASS (function))
11596 {
11597 case BUILT_IN_NORMAL:
11598 break;
11599 case BUILT_IN_MD:
11600 if (targetm.resolve_overloaded_builtin)
c2255bc4 11601 return targetm.resolve_overloaded_builtin (loc, function, params);
58646b77 11602 else
c22cacf3 11603 return NULL_TREE;
58646b77
PB
11604 default:
11605 return NULL_TREE;
11606 }
c22cacf3 11607
58646b77 11608 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
11609 switch (orig_code)
11610 {
86951993
AM
11611 case BUILT_IN_ATOMIC_EXCHANGE:
11612 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11613 case BUILT_IN_ATOMIC_LOAD:
11614 case BUILT_IN_ATOMIC_STORE:
11615 {
11616 /* Handle these 4 together so that they can fall through to the next
11617 case if the call is transformed to an _N variant. */
11618 switch (orig_code)
295844f6 11619 {
86951993
AM
11620 case BUILT_IN_ATOMIC_EXCHANGE:
11621 {
11622 if (resolve_overloaded_atomic_exchange (loc, function, params,
11623 &new_return))
11624 return new_return;
11625 /* Change to the _N variant. */
11626 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11627 break;
11628 }
11629
11630 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11631 {
11632 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11633 params,
11634 &new_return))
11635 return new_return;
11636 /* Change to the _N variant. */
11637 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11638 break;
11639 }
11640 case BUILT_IN_ATOMIC_LOAD:
11641 {
11642 if (resolve_overloaded_atomic_load (loc, function, params,
11643 &new_return))
11644 return new_return;
11645 /* Change to the _N variant. */
11646 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11647 break;
11648 }
11649 case BUILT_IN_ATOMIC_STORE:
11650 {
11651 if (resolve_overloaded_atomic_store (loc, function, params,
11652 &new_return))
11653 return new_return;
11654 /* Change to the _N variant. */
11655 orig_code = BUILT_IN_ATOMIC_STORE_N;
11656 break;
11657 }
11658 default:
11659 gcc_unreachable ();
295844f6 11660 }
86951993 11661 }
295844f6 11662 /* FALLTHRU */
86951993
AM
11663 case BUILT_IN_ATOMIC_EXCHANGE_N:
11664 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11665 case BUILT_IN_ATOMIC_LOAD_N:
11666 case BUILT_IN_ATOMIC_STORE_N:
295844f6
MP
11667 fetch_op = false;
11668 /* FALLTHRU */
86951993
AM
11669 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11670 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11671 case BUILT_IN_ATOMIC_AND_FETCH_N:
11672 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11673 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11674 case BUILT_IN_ATOMIC_OR_FETCH_N:
11675 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11676 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11677 case BUILT_IN_ATOMIC_FETCH_AND_N:
11678 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11679 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11680 case BUILT_IN_ATOMIC_FETCH_OR_N:
295844f6
MP
11681 orig_format = false;
11682 /* FALLTHRU */
e0a8ecf2
AM
11683 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11684 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11685 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11686 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11687 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11688 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11689 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11690 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11691 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11692 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11693 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11694 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11695 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11696 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11697 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11698 case BUILT_IN_SYNC_LOCK_RELEASE_N:
48ae6c13 11699 {
7a127fa7
MS
11700 /* The following are not _FETCH_OPs and must be accepted with
11701 pointers to _Bool (or C++ bool). */
11702 if (fetch_op)
11703 fetch_op =
11704 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11705 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11706 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11707 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11708
11709 int n = sync_resolve_size (function, params, fetch_op);
bbbbb16a 11710 tree new_function, first_param, result;
e79983f4 11711 enum built_in_function fncode;
48ae6c13
RH
11712
11713 if (n == 0)
11714 return error_mark_node;
11715
e79983f4
MM
11716 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11717 new_function = builtin_decl_explicit (fncode);
86951993
AM
11718 if (!sync_resolve_params (loc, function, new_function, params,
11719 orig_format))
48ae6c13
RH
11720 return error_mark_node;
11721
9771b263 11722 first_param = (*params)[0];
81e5eca8
MP
11723 result = build_function_call_vec (loc, vNULL, new_function, params,
11724 NULL);
86951993
AM
11725 if (result == error_mark_node)
11726 return result;
e0a8ecf2 11727 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
86951993 11728 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
2e6b45af
MS
11729 && orig_code != BUILT_IN_ATOMIC_STORE_N
11730 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
86951993 11731 result = sync_resolve_return (first_param, result, orig_format);
48ae6c13 11732
14ba7b28
MP
11733 if (fetch_op)
11734 /* Prevent -Wunused-value warning. */
11735 TREE_USED (result) = true;
11736
86951993
AM
11737 /* If new_return is set, assign function to that expr and cast the
11738 result to void since the generic interface returned void. */
11739 if (new_return)
11740 {
11741 /* Cast function result from I{1,2,4,8,16} to the required type. */
11742 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11743 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11744 result);
11745 TREE_SIDE_EFFECTS (result) = 1;
11746 protected_set_expr_location (result, loc);
11747 result = convert (void_type_node, result);
11748 }
48ae6c13
RH
11749 return result;
11750 }
11751
11752 default:
58646b77 11753 return NULL_TREE;
48ae6c13
RH
11754 }
11755}
11756
0af94e6f
JR
11757/* vector_types_compatible_elements_p is used in type checks of vectors
11758 values used as operands of binary operators. Where it returns true, and
11759 the other checks of the caller succeed (being vector types in he first
11760 place, and matching number of elements), we can just treat the types
11761 as essentially the same.
11762 Contrast with vector_targets_convertible_p, which is used for vector
11763 pointer types, and vector_types_convertible_p, which will allow
11764 language-specific matches under the control of flag_lax_vector_conversions,
11765 and might still require a conversion. */
11766/* True if vector types T1 and T2 can be inputs to the same binary
11767 operator without conversion.
11768 We don't check the overall vector size here because some of our callers
11769 want to give different error messages when the vectors are compatible
11770 except for the element count. */
11771
5bed876a 11772bool
0af94e6f 11773vector_types_compatible_elements_p (tree t1, tree t2)
5bed876a 11774{
0af94e6f
JR
11775 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11776 t1 = TREE_TYPE (t1);
11777 t2 = TREE_TYPE (t2);
11778
5bed876a
AH
11779 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11780
ab22c1fa
CF
11781 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11782 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11783 || c2 == FIXED_POINT_TYPE));
5bed876a 11784
0af94e6f
JR
11785 t1 = c_common_signed_type (t1);
11786 t2 = c_common_signed_type (t2);
5bed876a
AH
11787 /* Equality works here because c_common_signed_type uses
11788 TYPE_MAIN_VARIANT. */
0af94e6f
JR
11789 if (t1 == t2)
11790 return true;
11791 if (opaque && c1 == c2
11792 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11793 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11794 return true;
11795 return false;
5bed876a
AH
11796}
11797
104f8784
KG
11798/* Check for missing format attributes on function pointers. LTYPE is
11799 the new type or left-hand side type. RTYPE is the old type or
11800 right-hand side type. Returns TRUE if LTYPE is missing the desired
11801 attribute. */
11802
11803bool
11804check_missing_format_attribute (tree ltype, tree rtype)
11805{
11806 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11807 tree ra;
11808
11809 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11810 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11811 break;
11812 if (ra)
11813 {
11814 tree la;
11815 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11816 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11817 break;
11818 return !la;
11819 }
11820 else
11821 return false;
11822}
11823
ff6b6641
GDR
11824/* Subscripting with type char is likely to lose on a machine where
11825 chars are signed. So warn on any machine, but optionally. Don't
11826 warn for unsigned char since that type is safe. Don't warn for
11827 signed char because anyone who uses that must have done so
11828 deliberately. Furthermore, we reduce the false positive load by
11829 warning only for non-constant value of type char. */
11830
11831void
5bd012f8 11832warn_array_subscript_with_type_char (location_t loc, tree index)
ff6b6641
GDR
11833{
11834 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11835 && TREE_CODE (index) != INTEGER_CST)
5bd012f8
MP
11836 warning_at (loc, OPT_Wchar_subscripts,
11837 "array subscript has type %<char%>");
ff6b6641
GDR
11838}
11839
2a67bec2
ILT
11840/* Implement -Wparentheses for the unexpected C precedence rules, to
11841 cover cases like x + y << z which readers are likely to
11842 misinterpret. We have seen an expression in which CODE is a binary
100d537d
MLI
11843 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11844 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11845 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11846 expression was not formed using a binary or unary operator, or it
11847 was enclosed in parentheses. */
2a67bec2
ILT
11848
11849void
5d9de0d0 11850warn_about_parentheses (location_t loc, enum tree_code code,
fb3e178a 11851 enum tree_code code_left, tree arg_left,
100d537d 11852 enum tree_code code_right, tree arg_right)
2a67bec2
ILT
11853{
11854 if (!warn_parentheses)
11855 return;
11856
100d537d
MLI
11857 /* This macro tests that the expression ARG with original tree code
11858 CODE appears to be a boolean expression. or the result of folding a
11859 boolean expression. */
11860#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11861 (truth_value_p (TREE_CODE (ARG)) \
11862 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11863 /* Folding may create 0 or 1 integers from other expressions. */ \
11864 || ((CODE) != INTEGER_CST \
11865 && (integer_onep (ARG) || integer_zerop (ARG))))
11866
b8698a0f 11867 switch (code)
2a67bec2 11868 {
100d537d 11869 case LSHIFT_EXPR:
5d9de0d0
PC
11870 if (code_left == PLUS_EXPR)
11871 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11872 "suggest parentheses around %<+%> inside %<<<%>");
11873 else if (code_right == PLUS_EXPR)
11874 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11875 "suggest parentheses around %<+%> inside %<<<%>");
11876 else if (code_left == MINUS_EXPR)
11877 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11878 "suggest parentheses around %<-%> inside %<<<%>");
11879 else if (code_right == MINUS_EXPR)
11880 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11881 "suggest parentheses around %<-%> inside %<<<%>");
100d537d 11882 return;
2a67bec2 11883
100d537d 11884 case RSHIFT_EXPR:
5d9de0d0
PC
11885 if (code_left == PLUS_EXPR)
11886 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11887 "suggest parentheses around %<+%> inside %<>>%>");
11888 else if (code_right == PLUS_EXPR)
11889 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11890 "suggest parentheses around %<+%> inside %<>>%>");
11891 else if (code_left == MINUS_EXPR)
11892 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11893 "suggest parentheses around %<-%> inside %<>>%>");
11894 else if (code_right == MINUS_EXPR)
11895 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11896 "suggest parentheses around %<-%> inside %<>>%>");
100d537d 11897 return;
2a67bec2 11898
100d537d 11899 case TRUTH_ORIF_EXPR:
5d9de0d0
PC
11900 if (code_left == TRUTH_ANDIF_EXPR)
11901 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11902 "suggest parentheses around %<&&%> within %<||%>");
11903 else if (code_right == TRUTH_ANDIF_EXPR)
11904 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11905 "suggest parentheses around %<&&%> within %<||%>");
100d537d
MLI
11906 return;
11907
11908 case BIT_IOR_EXPR:
2a67bec2 11909 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
5d9de0d0
PC
11910 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11911 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11912 "suggest parentheses around arithmetic in operand of %<|%>");
11913 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11914 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11915 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11916 "suggest parentheses around arithmetic in operand of %<|%>");
2a67bec2 11917 /* Check cases like x|y==z */
5d9de0d0
PC
11918 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11919 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11920 "suggest parentheses around comparison in operand of %<|%>");
11921 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11922 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11923 "suggest parentheses around comparison in operand of %<|%>");
11924 /* Check cases like !x | y */
11925 else if (code_left == TRUTH_NOT_EXPR
11926 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11927 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11928 "suggest parentheses around operand of "
11929 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
100d537d 11930 return;
2a67bec2 11931
100d537d 11932 case BIT_XOR_EXPR:
2a67bec2 11933 if (code_left == BIT_AND_EXPR
5d9de0d0
PC
11934 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11935 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11936 "suggest parentheses around arithmetic in operand of %<^%>");
11937 else if (code_right == BIT_AND_EXPR
11938 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11939 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11940 "suggest parentheses around arithmetic in operand of %<^%>");
2a67bec2 11941 /* Check cases like x^y==z */
5d9de0d0
PC
11942 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11943 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11944 "suggest parentheses around comparison in operand of %<^%>");
11945 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11946 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11947 "suggest parentheses around comparison in operand of %<^%>");
11948 return;
2a67bec2 11949
100d537d 11950 case BIT_AND_EXPR:
5d9de0d0
PC
11951 if (code_left == PLUS_EXPR)
11952 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11953 "suggest parentheses around %<+%> in operand of %<&%>");
11954 else if (code_right == PLUS_EXPR)
11955 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11956 "suggest parentheses around %<+%> in operand of %<&%>");
5d9de0d0
PC
11957 else if (code_left == MINUS_EXPR)
11958 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11959 "suggest parentheses around %<-%> in operand of %<&%>");
11960 else if (code_right == MINUS_EXPR)
11961 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11962 "suggest parentheses around %<-%> in operand of %<&%>");
2a67bec2 11963 /* Check cases like x&y==z */
5d9de0d0
PC
11964 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11965 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11966 "suggest parentheses around comparison in operand of %<&%>");
11967 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11968 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11969 "suggest parentheses around comparison in operand of %<&%>");
11970 /* Check cases like !x & y */
11971 else if (code_left == TRUTH_NOT_EXPR
11972 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11973 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11974 "suggest parentheses around operand of "
11975 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
100d537d 11976 return;
2a67bec2 11977
100d537d 11978 case EQ_EXPR:
5d9de0d0
PC
11979 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11980 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11981 "suggest parentheses around comparison in operand of %<==%>");
11982 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11983 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11984 "suggest parentheses around comparison in operand of %<==%>");
11985 return;
11986 case NE_EXPR:
5d9de0d0
PC
11987 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11988 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11989 "suggest parentheses around comparison in operand of %<!=%>");
11990 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11991 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11992 "suggest parentheses around comparison in operand of %<!=%>");
11993 return;
11994
11995 default:
5d9de0d0
PC
11996 if (TREE_CODE_CLASS (code) == tcc_comparison)
11997 {
11998 if (TREE_CODE_CLASS (code_left) == tcc_comparison
fb3e178a
JJ
11999 && code_left != NE_EXPR && code_left != EQ_EXPR
12000 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
5d9de0d0
PC
12001 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12002 "comparisons like %<X<=Y<=Z%> do not "
12003 "have their mathematical meaning");
12004 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
fb3e178a 12005 && code_right != NE_EXPR && code_right != EQ_EXPR
5d9de0d0
PC
12006 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
12007 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12008 "comparisons like %<X<=Y<=Z%> do not "
12009 "have their mathematical meaning");
12010 }
100d537d 12011 return;
e7917d06 12012 }
100d537d 12013#undef NOT_A_BOOLEAN_EXPR_P
2a67bec2
ILT
12014}
12015
c616e51b
MLI
12016/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
12017
12018void
12019warn_for_unused_label (tree label)
12020{
12021 if (!TREE_USED (label))
12022 {
12023 if (DECL_INITIAL (label))
12024 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
12025 else
12026 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
12027 }
12028}
ff6b6641 12029
c9f9eb5d
AH
12030/* Warn for division by zero according to the value of DIVISOR. LOC
12031 is the location of the division operator. */
2e9cb75e
MLI
12032
12033void
c9f9eb5d 12034warn_for_div_by_zero (location_t loc, tree divisor)
2e9cb75e 12035{
ab22c1fa
CF
12036 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
12037 about division by zero. Do not issue a warning if DIVISOR has a
2e9cb75e
MLI
12038 floating-point type, since we consider 0.0/0.0 a valid way of
12039 generating a NaN. */
7d882b83 12040 if (c_inhibit_evaluation_warnings == 0
ab22c1fa 12041 && (integer_zerop (divisor) || fixed_zerop (divisor)))
c9f9eb5d 12042 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
2e9cb75e
MLI
12043}
12044
c1e1f433
BS
12045/* Warn for patterns where memset appears to be used incorrectly. The
12046 warning location should be LOC. ARG0, and ARG2 are the first and
12047 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
12048 each argument that was a literal zero. */
12049
12050void
12051warn_for_memset (location_t loc, tree arg0, tree arg2,
12052 int literal_zero_mask)
12053{
12054 if (warn_memset_transposed_args
12055 && integer_zerop (arg2)
12056 && (literal_zero_mask & (1 << 2)) != 0
12057 && (literal_zero_mask & (1 << 1)) == 0)
12058 warning_at (loc, OPT_Wmemset_transposed_args,
12059 "%<memset%> used with constant zero length "
12060 "parameter; this could be due to transposed "
12061 "parameters");
12062
12063 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
12064 {
12065 STRIP_NOPS (arg0);
12066 if (TREE_CODE (arg0) == ADDR_EXPR)
12067 arg0 = TREE_OPERAND (arg0, 0);
12068 tree type = TREE_TYPE (arg0);
b00e6e75 12069 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
c1e1f433
BS
12070 {
12071 tree elt_type = TREE_TYPE (type);
12072 tree domain = TYPE_DOMAIN (type);
12073 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
2fff3db8 12074 && domain != NULL_TREE
c1e1f433
BS
12075 && TYPE_MAXVAL (domain)
12076 && TYPE_MINVAL (domain)
12077 && integer_zerop (TYPE_MINVAL (domain))
12078 && integer_onep (fold_build2 (MINUS_EXPR, domain,
12079 arg2,
12080 TYPE_MAXVAL (domain))))
12081 warning_at (loc, OPT_Wmemset_elt_size,
12082 "%<memset%> used with length equal to "
12083 "number of elements without multiplication "
12084 "by element size");
12085 }
12086 }
12087}
12088
2d12797c
MLI
12089/* Subroutine of build_binary_op. Give warnings for comparisons
12090 between signed and unsigned quantities that may fail. Do the
12091 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
12092 so that casts will be considered, but default promotions won't
ba47d38d
AH
12093 be.
12094
12095 LOCATION is the location of the comparison operator.
2d12797c
MLI
12096
12097 The arguments of this function map directly to local variables
12098 of build_binary_op. */
12099
b8698a0f 12100void
ba47d38d 12101warn_for_sign_compare (location_t location,
b8698a0f
L
12102 tree orig_op0, tree orig_op1,
12103 tree op0, tree op1,
2d12797c
MLI
12104 tree result_type, enum tree_code resultcode)
12105{
12106 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
12107 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
12108 int unsignedp0, unsignedp1;
b8698a0f 12109
2d12797c
MLI
12110 /* In C++, check for comparison of different enum types. */
12111 if (c_dialect_cxx()
12112 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
12113 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
12114 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
ead51d36 12115 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
2d12797c 12116 {
ba47d38d
AH
12117 warning_at (location,
12118 OPT_Wsign_compare, "comparison between types %qT and %qT",
12119 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
2d12797c
MLI
12120 }
12121
12122 /* Do not warn if the comparison is being done in a signed type,
12123 since the signed type will only be chosen if it can represent
12124 all the values of the unsigned type. */
12125 if (!TYPE_UNSIGNED (result_type))
12126 /* OK */;
12127 /* Do not warn if both operands are unsigned. */
12128 else if (op0_signed == op1_signed)
12129 /* OK */;
12130 else
12131 {
ead51d36 12132 tree sop, uop, base_type;
2d12797c 12133 bool ovf;
ead51d36 12134
2d12797c
MLI
12135 if (op0_signed)
12136 sop = orig_op0, uop = orig_op1;
b8698a0f 12137 else
2d12797c
MLI
12138 sop = orig_op1, uop = orig_op0;
12139
b8698a0f 12140 STRIP_TYPE_NOPS (sop);
2d12797c 12141 STRIP_TYPE_NOPS (uop);
ead51d36
JJ
12142 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12143 ? TREE_TYPE (result_type) : result_type);
2d12797c
MLI
12144
12145 /* Do not warn if the signed quantity is an unsuffixed integer
12146 literal (or some static constant expression involving such
12147 literals or a conditional expression involving such literals)
12148 and it is non-negative. */
12149 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12150 /* OK */;
12151 /* Do not warn if the comparison is an equality operation, the
12152 unsigned quantity is an integral constant, and it would fit
12153 in the result if the result were signed. */
12154 else if (TREE_CODE (uop) == INTEGER_CST
12155 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
ead51d36 12156 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2d12797c
MLI
12157 /* OK */;
12158 /* In C, do not warn if the unsigned quantity is an enumeration
12159 constant and its maximum value would fit in the result if the
12160 result were signed. */
12161 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12162 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12163 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
ead51d36 12164 c_common_signed_type (base_type)))
2d12797c 12165 /* OK */;
b8698a0f 12166 else
ba47d38d 12167 warning_at (location,
b8698a0f 12168 OPT_Wsign_compare,
ba47d38d 12169 "comparison between signed and unsigned integer expressions");
2d12797c 12170 }
b8698a0f 12171
2d12797c
MLI
12172 /* Warn if two unsigned values are being compared in a size larger
12173 than their original size, and one (and only one) is the result of
12174 a `~' operator. This comparison will always fail.
b8698a0f 12175
2d12797c
MLI
12176 Also warn if one operand is a constant, and the constant does not
12177 have all bits set that are set in the ~ operand when it is
12178 extended. */
12179
828fb3ba
JM
12180 op0 = c_common_get_narrower (op0, &unsignedp0);
12181 op1 = c_common_get_narrower (op1, &unsignedp1);
b8698a0f 12182
2d12797c
MLI
12183 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12184 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12185 {
12186 if (TREE_CODE (op0) == BIT_NOT_EXPR)
828fb3ba 12187 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2d12797c 12188 if (TREE_CODE (op1) == BIT_NOT_EXPR)
828fb3ba 12189 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2d12797c 12190
9541ffee 12191 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2d12797c
MLI
12192 {
12193 tree primop;
12194 HOST_WIDE_INT constant, mask;
12195 int unsignedp;
12196 unsigned int bits;
b8698a0f 12197
9541ffee 12198 if (tree_fits_shwi_p (op0))
2d12797c
MLI
12199 {
12200 primop = op1;
12201 unsignedp = unsignedp1;
9439e9a1 12202 constant = tree_to_shwi (op0);
2d12797c
MLI
12203 }
12204 else
12205 {
12206 primop = op0;
12207 unsignedp = unsignedp0;
9439e9a1 12208 constant = tree_to_shwi (op1);
2d12797c 12209 }
b8698a0f 12210
2d12797c
MLI
12211 bits = TYPE_PRECISION (TREE_TYPE (primop));
12212 if (bits < TYPE_PRECISION (result_type)
12213 && bits < HOST_BITS_PER_LONG && unsignedp)
12214 {
e3fe09c1 12215 mask = HOST_WIDE_INT_M1U << bits;
2d12797c
MLI
12216 if ((mask & constant) != mask)
12217 {
12218 if (constant == 0)
0a756a3f
MP
12219 warning_at (location, OPT_Wsign_compare,
12220 "promoted ~unsigned is always non-zero");
2d12797c 12221 else
b8698a0f 12222 warning_at (location, OPT_Wsign_compare,
ba47d38d 12223 "comparison of promoted ~unsigned with constant");
2d12797c
MLI
12224 }
12225 }
12226 }
12227 else if (unsignedp0 && unsignedp1
12228 && (TYPE_PRECISION (TREE_TYPE (op0))
12229 < TYPE_PRECISION (result_type))
12230 && (TYPE_PRECISION (TREE_TYPE (op1))
12231 < TYPE_PRECISION (result_type)))
ba47d38d 12232 warning_at (location, OPT_Wsign_compare,
2d12797c
MLI
12233 "comparison of promoted ~unsigned with unsigned");
12234 }
12235}
12236
c5ee1358
MM
12237/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12238 type via c_common_type. If -Wdouble-promotion is in use, and the
12239 conditions for warning have been met, issue a warning. GMSGID is
12240 the warning message. It must have two %T specifiers for the type
12241 that was converted (generally "float") and the type to which it was
12242 converted (generally "double), respectively. LOC is the location
12243 to which the awrning should refer. */
12244
12245void
12246do_warn_double_promotion (tree result_type, tree type1, tree type2,
12247 const char *gmsgid, location_t loc)
12248{
12249 tree source_type;
12250
12251 if (!warn_double_promotion)
12252 return;
12253 /* If the conversion will not occur at run-time, there is no need to
12254 warn about it. */
12255 if (c_inhibit_evaluation_warnings)
12256 return;
12257 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12258 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12259 return;
12260 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12261 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12262 source_type = type1;
12263 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12264 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12265 source_type = type2;
12266 else
12267 return;
12268 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12269}
12270
da2e71c9
MLI
12271/* Possibly warn about unused parameters. */
12272
12273void
12274do_warn_unused_parameter (tree fn)
12275{
12276 tree decl;
12277
12278 for (decl = DECL_ARGUMENTS (fn);
12279 decl; decl = DECL_CHAIN (decl))
12280 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12281 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12282 && !TREE_NO_WARNING (decl))
12283 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12284 "unused parameter %qD", decl);
12285}
12286
12287
d0940d56
DS
12288/* Setup a TYPE_DECL node as a typedef representation.
12289
12290 X is a TYPE_DECL for a typedef statement. Create a brand new
12291 ..._TYPE node (which will be just a variant of the existing
12292 ..._TYPE node with identical properties) and then install X
12293 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12294
12295 The whole point here is to end up with a situation where each
12296 and every ..._TYPE node the compiler creates will be uniquely
12297 associated with AT MOST one node representing a typedef name.
12298 This way, even though the compiler substitutes corresponding
12299 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12300 early on, later parts of the compiler can always do the reverse
12301 translation and get back the corresponding typedef name. For
12302 example, given:
12303
12304 typedef struct S MY_TYPE;
12305 MY_TYPE object;
12306
12307 Later parts of the compiler might only know that `object' was of
12308 type `struct S' if it were not for code just below. With this
12309 code however, later parts of the compiler see something like:
12310
12311 struct S' == struct S
12312 typedef struct S' MY_TYPE;
12313 struct S' object;
12314
12315 And they can then deduce (from the node for type struct S') that
12316 the original object declaration was:
12317
12318 MY_TYPE object;
12319
12320 Being able to do this is important for proper support of protoize,
12321 and also for generating precise symbolic debugging information
12322 which takes full account of the programmer's (typedef) vocabulary.
12323
12324 Obviously, we don't want to generate a duplicate ..._TYPE node if
12325 the TYPE_DECL node that we are now processing really represents a
12326 standard built-in type. */
12327
12328void
12329set_underlying_type (tree x)
12330{
12331 if (x == error_mark_node)
12332 return;
42763690 12333 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
d0940d56
DS
12334 {
12335 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12336 TYPE_NAME (TREE_TYPE (x)) = x;
12337 }
12338 else if (TREE_TYPE (x) != error_mark_node
12339 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12340 {
12341 tree tt = TREE_TYPE (x);
12342 DECL_ORIGINAL_TYPE (x) = tt;
12343 tt = build_variant_type_copy (tt);
12344 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12345 TYPE_NAME (tt) = x;
12346 TREE_USED (tt) = TREE_USED (x);
12347 TREE_TYPE (x) = tt;
12348 }
12349}
12350
b646ba3f
DS
12351/* Record the types used by the current global variable declaration
12352 being parsed, so that we can decide later to emit their debug info.
12353 Those types are in types_used_by_cur_var_decl, and we are going to
12354 store them in the types_used_by_vars_hash hash table.
12355 DECL is the declaration of the global variable that has been parsed. */
12356
12357void
12358record_types_used_by_current_var_decl (tree decl)
12359{
12360 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12361
9771b263 12362 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
b646ba3f 12363 {
9771b263 12364 tree type = types_used_by_cur_var_decl->pop ();
bc87224e 12365 types_used_by_var_decl_insert (type, decl);
b646ba3f
DS
12366 }
12367}
12368
3797cb21
DS
12369/* If DECL is a typedef that is declared in the current function,
12370 record it for the purpose of -Wunused-local-typedefs. */
12371
12372void
12373record_locally_defined_typedef (tree decl)
12374{
12375 struct c_language_function *l;
12376
12377 if (!warn_unused_local_typedefs
12378 || cfun == NULL
12379 /* if this is not a locally defined typedef then we are not
12380 interested. */
12381 || !is_typedef_decl (decl)
12382 || !decl_function_context (decl))
12383 return;
12384
12385 l = (struct c_language_function *) cfun->language;
9771b263 12386 vec_safe_push (l->local_typedefs, decl);
3797cb21
DS
12387}
12388
12389/* If T is a TYPE_DECL declared locally, mark it as used. */
12390
12391void
12392maybe_record_typedef_use (tree t)
12393{
12394 if (!is_typedef_decl (t))
12395 return;
12396
12397 TREE_USED (t) = true;
12398}
12399
12400/* Warn if there are some unused locally defined typedefs in the
12401 current function. */
12402
12403void
12404maybe_warn_unused_local_typedefs (void)
12405{
12406 int i;
12407 tree decl;
12408 /* The number of times we have emitted -Wunused-local-typedefs
12409 warnings. If this is different from errorcount, that means some
12410 unrelated errors have been issued. In which case, we'll avoid
12411 emitting "unused-local-typedefs" warnings. */
12412 static int unused_local_typedefs_warn_count;
12413 struct c_language_function *l;
12414
12415 if (cfun == NULL)
12416 return;
12417
12418 if ((l = (struct c_language_function *) cfun->language) == NULL)
12419 return;
12420
12421 if (warn_unused_local_typedefs
12422 && errorcount == unused_local_typedefs_warn_count)
12423 {
9771b263 12424 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
3797cb21
DS
12425 if (!TREE_USED (decl))
12426 warning_at (DECL_SOURCE_LOCATION (decl),
12427 OPT_Wunused_local_typedefs,
12428 "typedef %qD locally defined but not used", decl);
12429 unused_local_typedefs_warn_count = errorcount;
12430 }
12431
9771b263 12432 vec_free (l->local_typedefs);
3797cb21
DS
12433}
12434
04159acf
MP
12435/* Warn about boolean expression compared with an integer value different
12436 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12437 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12438 are the operands of the comparison. The caller must ensure that
12439 either operand is a boolean expression. */
12440
12441void
12442maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12443 tree op1)
12444{
12445 if (TREE_CODE_CLASS (code) != tcc_comparison)
12446 return;
12447
f479b43d
JM
12448 tree f, cst;
12449 if (f = fold_for_warn (op0),
12450 TREE_CODE (f) == INTEGER_CST)
12451 cst = op0 = f;
12452 else if (f = fold_for_warn (op1),
12453 TREE_CODE (f) == INTEGER_CST)
12454 cst = op1 = f;
12455 else
04159acf
MP
12456 return;
12457
12458 if (!integer_zerop (cst) && !integer_onep (cst))
12459 {
716c0ba6
MP
12460 int sign = (TREE_CODE (op0) == INTEGER_CST
12461 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
04159acf
MP
12462 if (code == EQ_EXPR
12463 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12464 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12465 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12466 "with boolean expression is always false", cst);
12467 else
12468 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12469 "with boolean expression is always true", cst);
12470 }
716c0ba6
MP
12471 else if (integer_zerop (cst) || integer_onep (cst))
12472 {
577cd070
MP
12473 /* If the non-constant operand isn't of a boolean type, we
12474 don't want to warn here. */
12475 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12476 /* Handle booleans promoted to integers. */
25ff5dd3 12477 if (bool_promoted_to_int_p (noncst))
577cd070
MP
12478 /* Warn. */;
12479 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12480 && !truth_value_p (TREE_CODE (noncst)))
12481 return;
716c0ba6
MP
12482 /* Do some magic to get the right diagnostics. */
12483 bool flag = TREE_CODE (op0) == INTEGER_CST;
12484 flag = integer_zerop (cst) ? flag : !flag;
12485 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12486 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12487 "with boolean expression is always true", cst);
12488 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12489 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12490 "with boolean expression is always false", cst);
12491 }
04159acf
MP
12492}
12493
451b5e48
MP
12494/* Warn if signed left shift overflows. We don't warn
12495 about left-shifting 1 into the sign bit in C++14; cf.
12496 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12497 LOC is a location of the shift; OP0 and OP1 are the operands.
12498 Return true if an overflow is detected, false otherwise. */
12499
12500bool
12501maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12502{
12503 if (TREE_CODE (op0) != INTEGER_CST
12504 || TREE_CODE (op1) != INTEGER_CST)
12505 return false;
12506
12507 tree type0 = TREE_TYPE (op0);
12508 unsigned int prec0 = TYPE_PRECISION (type0);
12509
12510 /* Left-hand operand must be signed. */
12511 if (TYPE_UNSIGNED (type0))
12512 return false;
12513
b893e375
MP
12514 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12515 + TREE_INT_CST_LOW (op1));
c5404f1c
PB
12516 /* Handle the case of left-shifting 1 into the sign bit.
12517 * However, shifting 1 _out_ of the sign bit, as in
12518 * INT_MIN << 1, is considered an overflow.
12519 */
12520 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
451b5e48
MP
12521 {
12522 /* Never warn for C++14 onwards. */
12523 if (cxx_dialect >= cxx14)
12524 return false;
12525 /* Otherwise only if -Wshift-overflow=2. But return
12526 true to signal an overflow for the sake of integer
12527 constant expressions. */
12528 if (warn_shift_overflow < 2)
12529 return true;
12530 }
12531
451b5e48
MP
12532 bool overflowed = min_prec > prec0;
12533 if (overflowed && c_inhibit_evaluation_warnings == 0)
12534 warning_at (loc, OPT_Wshift_overflow_,
12535 "result of %qE requires %u bits to represent, "
12536 "but %qT only has %u bits",
12537 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12538 min_prec, type0, prec0);
12539
12540 return overflowed;
12541}
12542
c166b898
ILT
12543/* The C and C++ parsers both use vectors to hold function arguments.
12544 For efficiency, we keep a cache of unused vectors. This is the
12545 cache. */
12546
9771b263
DN
12547typedef vec<tree, va_gc> *tree_gc_vec;
12548static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
c166b898
ILT
12549
12550/* Return a new vector from the cache. If the cache is empty,
12551 allocate a new vector. These vectors are GC'ed, so it is OK if the
12552 pointer is not released.. */
12553
9771b263 12554vec<tree, va_gc> *
c166b898
ILT
12555make_tree_vector (void)
12556{
9771b263
DN
12557 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12558 return tree_vector_cache->pop ();
c166b898
ILT
12559 else
12560 {
9771b263 12561 /* Passing 0 to vec::alloc returns NULL, and our callers require
c166b898
ILT
12562 that we always return a non-NULL value. The vector code uses
12563 4 when growing a NULL vector, so we do too. */
9771b263
DN
12564 vec<tree, va_gc> *v;
12565 vec_alloc (v, 4);
12566 return v;
c166b898
ILT
12567 }
12568}
12569
12570/* Release a vector of trees back to the cache. */
12571
12572void
9771b263 12573release_tree_vector (vec<tree, va_gc> *vec)
c166b898
ILT
12574{
12575 if (vec != NULL)
12576 {
9771b263
DN
12577 vec->truncate (0);
12578 vec_safe_push (tree_vector_cache, vec);
c166b898
ILT
12579 }
12580}
12581
12582/* Get a new tree vector holding a single tree. */
12583
9771b263 12584vec<tree, va_gc> *
c166b898
ILT
12585make_tree_vector_single (tree t)
12586{
9771b263
DN
12587 vec<tree, va_gc> *ret = make_tree_vector ();
12588 ret->quick_push (t);
c166b898
ILT
12589 return ret;
12590}
12591
c12ff9d8
JM
12592/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12593
9771b263 12594vec<tree, va_gc> *
c12ff9d8
JM
12595make_tree_vector_from_list (tree list)
12596{
9771b263 12597 vec<tree, va_gc> *ret = make_tree_vector ();
c12ff9d8 12598 for (; list; list = TREE_CHAIN (list))
9771b263 12599 vec_safe_push (ret, TREE_VALUE (list));
c12ff9d8
JM
12600 return ret;
12601}
12602
c166b898
ILT
12603/* Get a new tree vector which is a copy of an existing one. */
12604
9771b263
DN
12605vec<tree, va_gc> *
12606make_tree_vector_copy (const vec<tree, va_gc> *orig)
c166b898 12607{
9771b263 12608 vec<tree, va_gc> *ret;
c166b898
ILT
12609 unsigned int ix;
12610 tree t;
12611
12612 ret = make_tree_vector ();
9771b263
DN
12613 vec_safe_reserve (ret, vec_safe_length (orig));
12614 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12615 ret->quick_push (t);
c166b898
ILT
12616 return ret;
12617}
12618
a9aa2c3a
NF
12619/* Return true if KEYWORD starts a type specifier. */
12620
12621bool
12622keyword_begins_type_specifier (enum rid keyword)
12623{
12624 switch (keyword)
12625 {
38b7bc7f 12626 case RID_AUTO_TYPE:
a9aa2c3a
NF
12627 case RID_INT:
12628 case RID_CHAR:
12629 case RID_FLOAT:
12630 case RID_DOUBLE:
12631 case RID_VOID:
a9aa2c3a
NF
12632 case RID_UNSIGNED:
12633 case RID_LONG:
12634 case RID_SHORT:
12635 case RID_SIGNED:
c65699ef 12636 CASE_RID_FLOATN_NX:
a9aa2c3a
NF
12637 case RID_DFLOAT32:
12638 case RID_DFLOAT64:
12639 case RID_DFLOAT128:
12640 case RID_FRACT:
12641 case RID_ACCUM:
12642 case RID_BOOL:
12643 case RID_WCHAR:
12644 case RID_CHAR16:
12645 case RID_CHAR32:
12646 case RID_SAT:
12647 case RID_COMPLEX:
12648 case RID_TYPEOF:
12649 case RID_STRUCT:
12650 case RID_CLASS:
12651 case RID_UNION:
12652 case RID_ENUM:
12653 return true;
12654 default:
78a7c317
DD
12655 if (keyword >= RID_FIRST_INT_N
12656 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12657 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12658 return true;
a9aa2c3a
NF
12659 return false;
12660 }
12661}
12662
12663/* Return true if KEYWORD names a type qualifier. */
12664
12665bool
12666keyword_is_type_qualifier (enum rid keyword)
12667{
12668 switch (keyword)
12669 {
12670 case RID_CONST:
12671 case RID_VOLATILE:
12672 case RID_RESTRICT:
267bac10 12673 case RID_ATOMIC:
a9aa2c3a
NF
12674 return true;
12675 default:
12676 return false;
12677 }
12678}
12679
12680/* Return true if KEYWORD names a storage class specifier.
12681
12682 RID_TYPEDEF is not included in this list despite `typedef' being
12683 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12684 such for syntactic convenience only. */
12685
12686bool
12687keyword_is_storage_class_specifier (enum rid keyword)
12688{
12689 switch (keyword)
12690 {
12691 case RID_STATIC:
12692 case RID_EXTERN:
12693 case RID_REGISTER:
12694 case RID_AUTO:
12695 case RID_MUTABLE:
12696 case RID_THREAD:
12697 return true;
12698 default:
12699 return false;
12700 }
12701}
12702
ba9e6dd5
NF
12703/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12704
12705static bool
12706keyword_is_function_specifier (enum rid keyword)
12707{
12708 switch (keyword)
12709 {
12710 case RID_INLINE:
bbceee64 12711 case RID_NORETURN:
ba9e6dd5
NF
12712 case RID_VIRTUAL:
12713 case RID_EXPLICIT:
12714 return true;
12715 default:
12716 return false;
12717 }
12718}
12719
12720/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12721 declaration-specifier (C99 6.7). */
12722
12723bool
12724keyword_is_decl_specifier (enum rid keyword)
12725{
12726 if (keyword_is_storage_class_specifier (keyword)
12727 || keyword_is_type_qualifier (keyword)
12728 || keyword_is_function_specifier (keyword))
12729 return true;
12730
12731 switch (keyword)
12732 {
12733 case RID_TYPEDEF:
12734 case RID_FRIEND:
12735 case RID_CONSTEXPR:
12736 return true;
12737 default:
12738 return false;
12739 }
12740}
12741
81f653d6
NF
12742/* Initialize language-specific-bits of tree_contains_struct. */
12743
12744void
12745c_common_init_ts (void)
12746{
12747 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12748 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
36536d79 12749 MARK_TS_TYPED (ARRAY_NOTATION_REF);
81f653d6
NF
12750}
12751
3ce4f9e4
ESR
12752/* Build a user-defined numeric literal out of an integer constant type VALUE
12753 with identifier SUFFIX. */
12754
12755tree
2d7aa578
ESR
12756build_userdef_literal (tree suffix_id, tree value,
12757 enum overflow_type overflow, tree num_string)
3ce4f9e4
ESR
12758{
12759 tree literal = make_node (USERDEF_LITERAL);
12760 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12761 USERDEF_LITERAL_VALUE (literal) = value;
2d7aa578 12762 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
3ce4f9e4
ESR
12763 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12764 return literal;
12765}
12766
f17a223d
RB
12767/* For vector[index], convert the vector to an array of the underlying type.
12768 Return true if the resulting ARRAY_REF should not be an lvalue. */
aa7da51a
JJ
12769
12770bool
f17a223d
RB
12771convert_vector_to_array_for_subscript (location_t loc,
12772 tree *vecp, tree index)
7edaa4d2 12773{
aa7da51a 12774 bool ret = false;
31521951 12775 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7edaa4d2
MG
12776 {
12777 tree type = TREE_TYPE (*vecp);
7edaa4d2 12778
aa7da51a 12779 ret = !lvalue_p (*vecp);
f17a223d 12780
7edaa4d2 12781 if (TREE_CODE (index) == INTEGER_CST)
cc269bb6 12782 if (!tree_fits_uhwi_p (index)
7d362f6c 12783 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7edaa4d2
MG
12784 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12785
f17a223d
RB
12786 /* We are building an ARRAY_REF so mark the vector as addressable
12787 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
12788 for function parameters. */
12789 c_common_mark_addressable_vec (*vecp);
12790
12791 *vecp = build1 (VIEW_CONVERT_EXPR,
12792 build_array_type_nelts (TREE_TYPE (type),
12793 TYPE_VECTOR_SUBPARTS (type)),
12794 *vecp);
7edaa4d2 12795 }
aa7da51a 12796 return ret;
7edaa4d2
MG
12797}
12798
a212e43f
MG
12799/* Determine which of the operands, if any, is a scalar that needs to be
12800 converted to a vector, for the range of operations. */
12801enum stv_conv
12802scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12803 bool complain)
12804{
12805 tree type0 = TREE_TYPE (op0);
12806 tree type1 = TREE_TYPE (op1);
12807 bool integer_only_op = false;
12808 enum stv_conv ret = stv_firstarg;
12809
31521951 12810 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
a212e43f
MG
12811 switch (code)
12812 {
12813 /* Most GENERIC binary expressions require homogeneous arguments.
12814 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12815 argument that is a vector and a second one that is a scalar, so
12816 we never return stv_secondarg for them. */
12817 case RSHIFT_EXPR:
12818 case LSHIFT_EXPR:
12819 if (TREE_CODE (type0) == INTEGER_TYPE
12820 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12821 {
68fca595 12822 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12823 {
12824 if (complain)
12825 error_at (loc, "conversion of scalar %qT to vector %qT "
12826 "involves truncation", type0, type1);
12827 return stv_error;
12828 }
12829 else
12830 return stv_firstarg;
12831 }
12832 break;
12833
12834 case BIT_IOR_EXPR:
12835 case BIT_XOR_EXPR:
12836 case BIT_AND_EXPR:
12837 integer_only_op = true;
191816a3 12838 /* fall through */
a212e43f 12839
93100c6b
MG
12840 case VEC_COND_EXPR:
12841
a212e43f
MG
12842 case PLUS_EXPR:
12843 case MINUS_EXPR:
12844 case MULT_EXPR:
12845 case TRUNC_DIV_EXPR:
12846 case CEIL_DIV_EXPR:
12847 case FLOOR_DIV_EXPR:
12848 case ROUND_DIV_EXPR:
12849 case EXACT_DIV_EXPR:
12850 case TRUNC_MOD_EXPR:
12851 case FLOOR_MOD_EXPR:
12852 case RDIV_EXPR:
12853 case EQ_EXPR:
12854 case NE_EXPR:
12855 case LE_EXPR:
12856 case GE_EXPR:
12857 case LT_EXPR:
12858 case GT_EXPR:
12859 /* What about UNLT_EXPR? */
31521951 12860 if (VECTOR_TYPE_P (type0))
a212e43f 12861 {
a212e43f 12862 ret = stv_secondarg;
6b4db501
MM
12863 std::swap (type0, type1);
12864 std::swap (op0, op1);
a212e43f
MG
12865 }
12866
12867 if (TREE_CODE (type0) == INTEGER_TYPE
12868 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12869 {
68fca595 12870 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12871 {
12872 if (complain)
12873 error_at (loc, "conversion of scalar %qT to vector %qT "
12874 "involves truncation", type0, type1);
12875 return stv_error;
12876 }
12877 return ret;
12878 }
12879 else if (!integer_only_op
12880 /* Allow integer --> real conversion if safe. */
12881 && (TREE_CODE (type0) == REAL_TYPE
12882 || TREE_CODE (type0) == INTEGER_TYPE)
12883 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12884 {
68fca595 12885 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12886 {
12887 if (complain)
12888 error_at (loc, "conversion of scalar %qT to vector %qT "
12889 "involves truncation", type0, type1);
12890 return stv_error;
12891 }
12892 return ret;
12893 }
12894 default:
12895 break;
12896 }
12897
12898 return stv_nothing;
12899}
12900
af63ba4b
JM
12901/* Return the alignment of std::max_align_t.
12902
12903 [support.types.layout] The type max_align_t is a POD type whose alignment
12904 requirement is at least as great as that of every scalar type, and whose
12905 alignment requirement is supported in every context. */
12906
12907unsigned
12908max_align_t_align ()
12909{
63012d9a
JM
12910 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
12911 TYPE_ALIGN (long_double_type_node));
12912 if (float128_type_node != NULL_TREE)
12913 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
12914 return max_align;
af63ba4b
JM
12915}
12916
e28d52cf
DS
12917/* Return true iff ALIGN is an integral constant that is a fundamental
12918 alignment, as defined by [basic.align] in the c++-11
12919 specifications.
12920
12921 That is:
12922
12923 [A fundamental alignment is represented by an alignment less than or
12924 equal to the greatest alignment supported by the implementation
af63ba4b 12925 in all contexts, which is equal to alignof(max_align_t)]. */
e28d52cf
DS
12926
12927bool
af63ba4b 12928cxx_fundamental_alignment_p (unsigned align)
e28d52cf 12929{
af63ba4b 12930 return (align <= max_align_t_align ());
e28d52cf
DS
12931}
12932
f04dda30
MP
12933/* Return true if T is a pointer to a zero-sized aggregate. */
12934
12935bool
12936pointer_to_zero_sized_aggr_p (tree t)
12937{
12938 if (!POINTER_TYPE_P (t))
12939 return false;
12940 t = TREE_TYPE (t);
12941 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12942}
12943
1807ffc1
MS
12944/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12945 with no library fallback or for an ADDR_EXPR whose operand is such type
12946 issues an error pointing to the location LOC.
12947 Returns true when the expression has been diagnosed and false
12948 otherwise. */
12949bool
12950reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12951{
12952 if (TREE_CODE (expr) == ADDR_EXPR)
12953 expr = TREE_OPERAND (expr, 0);
12954
12955 if (TREE_TYPE (expr)
12956 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
1be56bc5 12957 && TREE_CODE (expr) == FUNCTION_DECL
1807ffc1
MS
12958 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12959 false positives for user-declared built-ins such as abs or
12960 strlen, and for C++ operators new and delete.
12961 The c_decl_implicit() test avoids false positives for implicitly
12962 declared built-ins with library fallbacks (such as abs). */
12963 && DECL_BUILT_IN (expr)
12964 && DECL_IS_BUILTIN (expr)
12965 && !c_decl_implicit (expr)
12966 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12967 {
12968 if (loc == UNKNOWN_LOCATION)
12969 loc = EXPR_LOC_OR_LOC (expr, input_location);
12970
12971 /* Reject arguments that are built-in functions with
12972 no library fallback. */
12973 error_at (loc, "built-in function %qE must be directly called", expr);
12974
12975 return true;
12976 }
12977
12978 return false;
12979}
12980
3e3b8d63
MP
12981/* If we're creating an if-else-if condition chain, first see if we
12982 already have this COND in the CHAIN. If so, warn and don't add COND
12983 into the vector, otherwise add the COND there. LOC is the location
12984 of COND. */
12985
12986void
12987warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12988{
12989 /* No chain has been created yet. Do nothing. */
12990 if (*chain == NULL)
12991 return;
12992
12993 if (TREE_SIDE_EFFECTS (cond))
12994 {
12995 /* Uh-oh! This condition has a side-effect, thus invalidates
12996 the whole chain. */
12997 delete *chain;
12998 *chain = NULL;
12999 return;
13000 }
13001
13002 unsigned int ix;
13003 tree t;
13004 bool found = false;
13005 FOR_EACH_VEC_ELT (**chain, ix, t)
13006 if (operand_equal_p (cond, t, 0))
13007 {
13008 if (warning_at (loc, OPT_Wduplicated_cond,
13009 "duplicated %<if%> condition"))
13010 inform (EXPR_LOCATION (t), "previously used here");
13011 found = true;
13012 break;
13013 }
13014
13015 if (!found
13016 && !CONSTANT_CLASS_P (cond)
13017 /* Don't infinitely grow the chain. */
13018 && (*chain)->length () < 512)
13019 (*chain)->safe_push (cond);
13020}
13021
e78bede6
MP
13022/* Check if array size calculations overflow or if the array covers more
13023 than half of the address space. Return true if the size of the array
13024 is valid, false otherwise. TYPE is the type of the array and NAME is
13025 the name of the array, or NULL_TREE for unnamed arrays. */
13026
13027bool
13028valid_array_size_p (location_t loc, tree type, tree name)
13029{
13030 if (type != error_mark_node
13031 && COMPLETE_TYPE_P (type)
13032 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
13033 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
13034 {
13035 if (name)
13036 error_at (loc, "size of array %qE is too large", name);
13037 else
13038 error_at (loc, "size of unnamed array is too large");
13039 return false;
13040 }
13041 return true;
13042}
13043
174f6622
ES
13044/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
13045 timestamp to replace embedded current dates to get reproducible
13046 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
15c98b2e 13047
174f6622 13048time_t
15c98b2e 13049cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
174f6622
ES
13050{
13051 char *source_date_epoch;
fe55692c 13052 int64_t epoch;
174f6622
ES
13053 char *endptr;
13054
13055 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
13056 if (!source_date_epoch)
13057 return (time_t) -1;
13058
13059 errno = 0;
fe55692c
JDA
13060#if defined(INT64_T_IS_LONG)
13061 epoch = strtol (source_date_epoch, &endptr, 10);
13062#else
174f6622 13063 epoch = strtoll (source_date_epoch, &endptr, 10);
fe55692c 13064#endif
15c98b2e
ES
13065 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
13066 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
13067 {
13068 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
13069 "expand to a non-negative integer less than or equal to %wd",
13070 MAX_SOURCE_DATE_EPOCH);
13071 return (time_t) -1;
13072 }
174f6622
ES
13073
13074 return (time_t) epoch;
13075}
13076
cb18fd07
DM
13077/* Callback for libcpp for offering spelling suggestions for misspelled
13078 directives. GOAL is an unrecognized string; CANDIDATES is a
13079 NULL-terminated array of candidate strings. Return the closest
13080 match to GOAL within CANDIDATES, or NULL if none are good
13081 suggestions. */
13082
13083const char *
13084cb_get_suggestion (cpp_reader *, const char *goal,
13085 const char *const *candidates)
13086{
13087 best_match<const char *, const char *> bm (goal);
13088 while (*candidates)
13089 bm.consider (*candidates++);
13090 return bm.get_best_meaningful_candidate ();
13091}
13092
5c3a10fb
MP
13093/* Check and possibly warn if two declarations have contradictory
13094 attributes, such as always_inline vs. noinline. */
13095
13096bool
13097diagnose_mismatched_attributes (tree olddecl, tree newdecl)
13098{
13099 bool warned = false;
13100
13101 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
13102 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
13103 /* An optimization attribute applied on a declaration after the
13104 definition is likely not what the user wanted. */
13105 if (a2 != NULL_TREE
13106 && DECL_SAVED_TREE (olddecl) != NULL_TREE
13107 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
13108 warned |= warning (OPT_Wattributes,
13109 "optimization attribute on %qD follows "
13110 "definition but the attribute doesn%'t match",
13111 newdecl);
13112
13113 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
13114 if (DECL_DECLARED_INLINE_P (newdecl)
13115 && DECL_UNINLINABLE (olddecl)
13116 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
13117 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
13118 "declaration with attribute noinline", newdecl);
13119 else if (DECL_DECLARED_INLINE_P (olddecl)
13120 && DECL_UNINLINABLE (newdecl)
13121 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
13122 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13123 "noinline follows inline declaration ", newdecl);
13124 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
13125 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
13126 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13127 "%qs follows declaration with attribute %qs",
13128 newdecl, "noinline", "always_inline");
13129 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
13130 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
13131 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13132 "%qs follows declaration with attribute %qs",
13133 newdecl, "always_inline", "noinline");
13134 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
13135 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
13136 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13137 "%qs follows declaration with attribute %qs",
13138 newdecl, "cold", "hot");
13139 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
13140 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
13141 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13142 "%qs follows declaration with attribute %qs",
13143 newdecl, "hot", "cold");
13144 return warned;
13145}
13146
39dabefd 13147#include "gt-c-family-c-common.h"